sonar-delphi
sonar-delphi copied to clipboard
Bug: PointerName rule wrong issue when type name startswith T without prefix
Prerequisites
- [X] This bug is in SonarDelphi, not SonarQube or my Delphi code.
- [X] This bug has not already been reported.
SonarDelphi version
1.5.0
SonarQube version
10.4.1.88267
Issue description
The PointerName rule checks whether pointers follow the naming convention. According to the rule "Why is this an issue?" example:
- ^TObject should be named PObject
- ^TMyType should be named PMyType
Using the T prefix for type/class/record names is good practice, but it is something that will not always be followed. Rules that validate type/class/record naming can have a prefix setting other than T or can be disabled.
Based on the context above, if a type is defined without the T prefix, but the type name starts with T, the PointerName rule report will be invalid.
For example, a pointer called PTaskInfo for the custom type TaskInfo, which does not have the prefix T, but its name starts with T, will have an invalid issue reported by the PointerName rule.
I believe the problem can be fixed by changing the check done here, to something like:
private static String expectedPointerName(String dereferencedName) {
return "P" + dereferencedName.substring(dereferencedName.startsWith("T") &&
dereferencedName.length() > 1 &&
Character.isUpperCase(dereferencedName.charAt(1)) ? 1 : 0);
}
Is my thought valid? Does the proposed fix make sense? In this case, I would like to open a PR with the correction, I'm happy to receive your feedback and suggestions.
Steps to reproduce
Run Sonar Delphi with the code exemplified in this issue.
Minimal Delphi code exhibiting the issue
unit MyTestUnit;
interface
type
TaskInfo = record
title : string;
assignee : string;
end;
PTaskInfo = ^TaskInfo;
implementation
begin
end.