-
Notifications
You must be signed in to change notification settings - Fork 231
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
FIX S2252 FP: Rule raises issue in case of non integer value in for loop. #5523
The head ref may contain hidden characters: "\u010Daba/S2252"
Conversation
analyzers/src/SonarAnalyzer.CSharp/Rules/ForLoopConditionAlwaysFalse.cs
Outdated
Show resolved
Hide resolved
{ | ||
var loopInitializerValues = new Dictionary<string, int>(); | ||
var loopInitializerValues = new Dictionary<string, double>(); | ||
if (variableDeclarationSyntax != null) | ||
{ | ||
foreach (var variableDeclaration in variableDeclarationSyntax.Variables) | ||
{ | ||
if (variableDeclaration.Initializer is EqualsValueClauseSyntax initializer |
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.
We can simplify this to if (variableDeclaration.Initializer is { } initializer
&& initializer is AssignmentExpressionSyntax assignment | ||
&& assignment.Left is SimpleNameSyntax simpleName |
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.
pedantic: we can use pattern matching and simplify this a bit:
&& initializer is AssignmentExpressionSyntax { Left: SimpleNameSyntax simpleName } assignment
analyzers/tests/SonarAnalyzer.UnitTest/TestCases/ForLoopConditionAlwaysFalse.cs
Show resolved
Hide resolved
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.
LGTM!
64beaf6
to
bef9c9e
Compare
Kudos, SonarCloud Quality Gate passed! |
Kudos, SonarCloud Quality Gate passed! |
Fixes #5428