Skip to content
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 S1450 FP when field is assigned value in event handler #9510

Merged
merged 4 commits into from
Jul 11, 2024

Conversation

sebastien-marichal
Copy link
Contributor

Fixes #8239

These changes treat method groups as invocation.

@sebastien-marichal sebastien-marichal force-pushed the sma/S1450-event-handler branch 3 times, most recently from 81930cd to 50ab407 Compare July 9, 2024 11:21
Copy link
Contributor

@mary-georgiou-sonarsource mary-georgiou-sonarsource left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Details and one question for extra tests.

if (privateFields.Keys.Any(x => x.Name == node.Identifier.ValueText))
var memberReference = GetTopmostSyntaxWithTheSameSymbol(node);
if (memberReference.Symbol is IFieldSymbol fieldSymbol
&& privateFields.ContainsKey(fieldSymbol))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can go to the previous line

&& memberReference.Symbol is IMethodSymbol
&& GetParentPseudoStatement(memberReference) is { } pseudoStatement)
else if (memberReference.Symbol is IMethodSymbol
&& GetParentPseudoStatement(memberReference) is { } pseudoStatement)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

{
// Adding method group to the invocation list
invocations.GetOrAdd(pseudoStatement, _ => new HashSet<ISymbol>())
.Add(memberReference.Symbol);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

{
// If the field is not static and is not from the current instance we
// consider the reference as read.
if (!fieldReference.Symbol.IsStatic
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same - one line

Comment on lines 482 to 483
private void Increment(int dummy)
=> _wasCalled = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private void Increment(int dummy)
=> _wasCalled = true;
private void Increment(int dummy) =>
_wasCalled = true;

}
}

private void Increment(int dummy)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible ever to have such method in another class/assembly?
I know that fields should be accessible in the class it self only - but just to be sure/.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it is possible.
If the field is updated outside the class, it is out of scope of this rule, IMO. And very peculiar, I am thinking of using unsafe code or reflection.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

Copy link

sonarcloud bot commented Jul 11, 2024

Copy link

sonarcloud bot commented Jul 11, 2024

Copy link
Contributor

@mary-georgiou-sonarsource mary-georgiou-sonarsource left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@mary-georgiou-sonarsource mary-georgiou-sonarsource merged commit 885074e into master Jul 11, 2024
29 checks passed
@mary-georgiou-sonarsource mary-georgiou-sonarsource deleted the sma/S1450-event-handler branch July 11, 2024 14:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix S1450 FP when field is assigned value in event handler
2 participants