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

Improve S1144: highlight only identifiers #6679

Merged
merged 20 commits into from
Feb 6, 2023

Conversation

zsolt-kolbay-sonarsource
Copy link
Contributor

@zsolt-kolbay-sonarsource zsolt-kolbay-sonarsource commented Jan 26, 2023

Fixes #6321

@@ -215,24 +216,29 @@ variableDeclarator.Parent.Parent switch
};

Diagnostic CreateS1144Diagnostic(SyntaxNode syntaxNode, ISymbol symbol) =>
Diagnostic.Create(RuleS1144, syntaxNode.GetLocation(), accessibility, symbol.GetClassification(), GetMemberName(symbol));
Diagnostic.Create(RuleS1144, GetIssueLocation(syntaxNode), accessibility, symbol.GetClassification(), GetMemberName(symbol));

Choose a reason for hiding this comment

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

We need to discuss what to do about the fadeOutCode argument of the diagnostic. The best solution would be to fade out the entire method (current behavior) while squiggling only the method name at the same time. I don't think this is possible but you may be able to find a good workaround.

Copy link
Contributor

Choose a reason for hiding this comment

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

can you bring more detail on this, @martin-strecker-sonarsource ?

I looked in the (both the version we use and the latest) and could not find this argument

Copy link
Contributor

Choose a reason for hiding this comment

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

This is what it looks like for me for unused methods in VS:
image

The fadeOutCode parameter dims the whole method which looks nice (see e.g. the difference between private and protected). But at the same time, all the squiggles are much too offensive.
So for me fading out the whole method while only squiggling the method name (or don't squiggle at all) would be the best compromise.

If we only squiggle e.g. the method name I would remove the fadeOutCode option because that would most likely look awkward.

I'm pretty sure that fading out is only supported by VS as it is just a custom property with a special name added to the diagnostic.

It is set here:

private static readonly DiagnosticDescriptor RuleS1144 = DescriptorFactory.Create(S1144DiagnosticId, S1144MessageFormat, fadeOutCode: true);
private static readonly DiagnosticDescriptor RuleS4487 = DescriptorFactory.Create(S4487DiagnosticId, S4487MessageFormat, fadeOutCode: true);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you, that's good to know 😃 I actually thought we were doing the squiggly and VS is doing the fading out by itself.

I checked the code, fadeoutCode is translated to WellKnownDiagnosticTags.Unnecessary and it applies to the entire Diagnostic. I don't see any way of applying it partially. The only way I see to do this is to raise a secondary issue, which only does the fading of the code. But that sounds like an awkward solution. So I simply removed the fade out effect for S1144.
What do think?

Choose a reason for hiding this comment

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

If you want to go crazy, you can do what Roslyn does in such a case:
https://github.com/dotnet/roslyn/blob/1d8f8cd8be6581a03e8379b8f146dcb5f11800c9/src/Analyzers/Core/Analyzers/Helpers/DiagnosticHelper.cs#L64-L105

See how additionalUnnecessaryLocations is handled. But you may ask @andrei-epure-sonarsource if he sees the need for us to support a scenario where the fadeout is different from the squiggles.

Copy link
Contributor

Choose a reason for hiding this comment

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

this is nice and interesting to see @martin-strecker-sonarsource !

I would favor to keep this PR simple - adding this new functionality to the analyzer should come with a change in the testing framework too, and we'd go too wide for this issue

I think it would be a cool thing, but I would also check where else this might help and do the updates to all the rules - this should probably be an MMF on improving visibility and useability of issues inside IDEs (feel free to discuss it with @tom-howlett-sonarsource and mention the idea to the bubble)

Choose a reason for hiding this comment

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

I created #6697

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you. I disabled the fade-out effect for now for both rules.

Copy link
Contributor

@andrei-epure-sonarsource andrei-epure-sonarsource left a comment

Choose a reason for hiding this comment

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

great work!

I left some suggestions

Copy link
Contributor

Choose a reason for hiding this comment

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

There might be some cases that don't work as intended.

Copy link
Contributor

@andrei-epure-sonarsource andrei-epure-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, great work on this one!

Thanks @martin-strecker-sonarsource for joining the review!

Comment on lines 240 to 241
private int Calculated => 1; // Noncompliant {{Remove the unused private property 'Calculated'.}}
// ^^^^^^^^^^
Copy link
Contributor

Choose a reason for hiding this comment

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

I took a quick look and it seems that arrow expressions are missing in general. Could you add these after checking if the test cases are really missing?

Prop { get => 1; }
Prop { set => _Prop = value; }

Indexer are also missing and most likely are FPs

private int this[int i] => 1;
private int this[int i] { get => 1; }
private int this[int i] { set => _ = value; }
private int this[int i] { get { return 1; } set { _ = value; } }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch. I've changed the code so that it only highlights the this keyword for unused indexers.
Also added test cases for expression-bodied properties.

Copy link
Contributor

Choose a reason for hiding this comment

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

Some test cases might be missing.

…Member.CSharp10.cs

Co-authored-by: Martin Strecker <103252490+martin-strecker-sonarsource@users.noreply.github.com>
Copy link
Contributor

Choose a reason for hiding this comment

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

GetIdentifier should return the this token for an indexer declaration.

@sonarqubecloud
Copy link

sonarqubecloud bot commented Feb 6, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

@sonarqubecloud
Copy link

sonarqubecloud bot commented Feb 6, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

100.0% 100.0% Coverage
0.0% 0.0% Duplication

Copy link
Contributor

Choose a reason for hiding this comment

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

LGTM.

@andrei-epure-sonarsource andrei-epure-sonarsource merged commit 6d979a0 into master Feb 6, 2023
@andrei-epure-sonarsource andrei-epure-sonarsource deleted the Zsolt/S1144-FP-highlight-identifier branch February 6, 2023 10:38
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.

S1144: Highlight only the identifier name
3 participants