-
Notifications
You must be signed in to change notification settings - Fork 227
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rule S1125: Boolean literals should not be redundant #2267
Conversation
sonaranalyzer-dotnet/src/SonarAnalyzer.VisualBasic/Rules/BooleanLiteralUnnecessary.cs
Show resolved
Hide resolved
8478b6f
to
c9f7ea5
Compare
sonaranalyzer-dotnet/src/SonarAnalyzer.Common/Rules/BooleanLiteralUnnecessaryBase.cs
Outdated
Show resolved
Hide resolved
sonaranalyzer-dotnet/src/SonarAnalyzer.Common/Rules/BooleanLiteralUnnecessaryBase.cs
Outdated
Show resolved
Hide resolved
sonaranalyzer-dotnet/src/SonarAnalyzer.Common/Rules/BooleanLiteralUnnecessaryBase.cs
Outdated
Show resolved
Hide resolved
sonaranalyzer-dotnet/src/SonarAnalyzer.Common/Rules/BooleanLiteralUnnecessaryBase.cs
Outdated
Show resolved
Hide resolved
sonaranalyzer-dotnet/src/SonarAnalyzer.Common/Rules/BooleanLiteralUnnecessaryBase.cs
Outdated
Show resolved
Hide resolved
sonaranalyzer-dotnet/src/SonarAnalyzer.Common/Rules/BooleanLiteralUnnecessaryBase.cs
Outdated
Show resolved
Hide resolved
sonaranalyzer-dotnet/src/SonarAnalyzer.Common/Rules/BooleanLiteralUnnecessaryBase.cs
Outdated
Show resolved
Hide resolved
sonaranalyzer-dotnet/src/SonarAnalyzer.Common/Rules/BooleanLiteralUnnecessaryBase.cs
Outdated
Show resolved
Hide resolved
sonaranalyzer-dotnet/src/SonarAnalyzer.Common/Rules/BooleanLiteralUnnecessaryBase.cs
Outdated
Show resolved
Hide resolved
sonaranalyzer-dotnet/src/SonarAnalyzer.Common/Rules/BooleanLiteralUnnecessaryBase.cs
Outdated
Show resolved
Hide resolved
2adce83
to
0026dee
Compare
internal const string DiagnosticId = "S1125"; | ||
protected const string MessageFormat = "Remove the unnecessary Boolean literal(s)."; | ||
|
||
protected abstract TSyntaxKind TrueLiteral { get; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of asking sub-classes to provide TrueLiteral
, FalseLiteral
and IsKind
you could require only IsTrueLiteralKind
and IsFalseLiteralKind
methods
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The TSyntaxKind
is passed as argument to many methods: CheckForBooleanConstantOnLeft
, CheckForBooleanConstantOnRight
and CheckForBooleanConstant
. I think the code is readable enough as is
sonaranalyzer-dotnet/src/SonarAnalyzer.Common/Rules/BooleanLiteralUnnecessaryBase.cs
Outdated
Show resolved
Hide resolved
sonaranalyzer-dotnet/src/SonarAnalyzer.Common/Rules/BooleanLiteralUnnecessaryBase.cs
Outdated
Show resolved
Hide resolved
sonaranalyzer-dotnet/src/SonarAnalyzer.Common/Rules/BooleanLiteralUnnecessaryBase.cs
Outdated
Show resolved
Hide resolved
sonaranalyzer-dotnet/src/SonarAnalyzer.VisualBasic/Rules/BooleanLiteralUnnecessary.cs
Outdated
Show resolved
Hide resolved
sonaranalyzer-dotnet/src/SonarAnalyzer.VisualBasic/Rules/BooleanLiteralUnnecessary.cs
Outdated
Show resolved
Hide resolved
x = false; // Fixed | ||
x = Foo(); // Fixed | ||
x = Foo(); // Fixed | ||
x = Foo(); // Fixed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[cosmetic] Could you align the // Fixed
comments. This will means that the // Noncompliant
are also aligned.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, it is not possible for both //Noncompliant
and //Fixed
to be aligned.
- the
// Noncompliant
are aligned - the
// Fixed
are not aligned because code is being cut out, andFalse
has 5 letters,True
has 4 letters, thus the lines cannot be aligned between themselves in both files
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aaaah!
Dim exp = True | ||
Dim exp2 = True | ||
|
||
Dim z = True OrElse ((True)) ' Noncompliant (also S1764) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you also add a check with And
and Or
(the operators forcing both parts to be evaluated).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
really good catch, thanks!!!
sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/BooleanLiteralUnnecessary.vb
Show resolved
Hide resolved
60f7501
to
bd5d123
Compare
Fix #2061