-
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
Update S2328: "GetHashCode should not reference mutable fields" should report once per method #414
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
|
||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using Microsoft.CodeAnalysis; | ||
|
@@ -26,7 +27,6 @@ | |
using Microsoft.CodeAnalysis.Diagnostics; | ||
using SonarAnalyzer.Common; | ||
using SonarAnalyzer.Helpers; | ||
using System.Collections.Generic; | ||
|
||
namespace SonarAnalyzer.Rules.CSharp | ||
{ | ||
|
@@ -35,10 +35,11 @@ namespace SonarAnalyzer.Rules.CSharp | |
public class GetHashCodeMutable : SonarDiagnosticAnalyzer | ||
{ | ||
internal const string DiagnosticId = "S2328"; | ||
private const string MessageFormat = "Remove this use of '{0}' from the 'GetHashCode' declaration, or make it 'readonly'."; | ||
private const string IssueMessage = " Refactor 'GetHashCode' to not reference mutable fields."; | ||
private const string SecondaryMessageFormat = "Remove this use of '{0}' or make it 'readonly'."; | ||
|
||
private static readonly DiagnosticDescriptor rule = | ||
DiagnosticDescriptorBuilder.GetDescriptor(DiagnosticId, MessageFormat, RspecStrings.ResourceManager); | ||
DiagnosticDescriptorBuilder.GetDescriptor(DiagnosticId, IssueMessage, RspecStrings.ResourceManager); | ||
|
||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(rule); | ||
|
||
|
@@ -50,9 +51,7 @@ protected sealed override void Initialize(SonarAnalysisContext context) | |
var methodSyntax = (MethodDeclarationSyntax)c.Node; | ||
var methodSymbol = c.SemanticModel.GetDeclaredSymbol(methodSyntax); | ||
|
||
if (methodSymbol == null || | ||
methodSyntax.Identifier.Text != "GetHashCode" || | ||
!methodSyntax.Modifiers.Any(SyntaxKind.OverrideKeyword)) | ||
if (!methodSymbol.IsObjectGetHashCode()) | ||
{ | ||
return; | ||
} | ||
|
@@ -74,15 +73,25 @@ protected sealed override void Initialize(SonarAnalysisContext context) | |
.Where(symbol => symbol != null) | ||
.ToImmutableHashSet(); | ||
|
||
var identifiers = methodSyntax.DescendantNodes() | ||
.OfType<IdentifierNameSyntax>(); | ||
var identifiers = methodSyntax.DescendantNodes().OfType<IdentifierNameSyntax>(); | ||
|
||
var secondaryLocations = GetAllFirstMutableFieldsUsed(c, fieldsOfClass, identifiers) | ||
.Select(identifierSyntax => new SecondaryLocation(identifierSyntax.GetLocation(), | ||
string.Format(SecondaryMessageFormat, identifierSyntax.Identifier.Text))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This Select is a bit ugly and could be extracted into a method... |
||
.ToList(); | ||
if (secondaryLocations.Count == 0) | ||
{ | ||
return; | ||
} | ||
|
||
ReportOnFirstReferences(c, fieldsOfClass, identifiers); | ||
c.ReportDiagnostic(Diagnostic.Create(rule, methodSyntax.Identifier.GetLocation(), | ||
additionalLocations: secondaryLocations.ToAdditionalLocations(), | ||
properties: secondaryLocations.ToProperties())); | ||
}, | ||
SyntaxKind.MethodDeclaration); | ||
} | ||
|
||
private static void ReportOnFirstReferences(SyntaxNodeAnalysisContext context, | ||
private static IEnumerable<IdentifierNameSyntax> GetAllFirstMutableFieldsUsed(SyntaxNodeAnalysisContext context, | ||
ImmutableHashSet<IFieldSymbol> fieldsOfClass, IEnumerable<IdentifierNameSyntax> identifiers) | ||
{ | ||
var syntaxNodes = new Dictionary<IFieldSymbol, List<IdentifierNameSyntax>>(); | ||
|
@@ -108,12 +117,9 @@ private static void ReportOnFirstReferences(SyntaxNodeAnalysisContext context, | |
syntaxNodes[identifierSymbol].Add(identifier); | ||
} | ||
|
||
foreach (var identifierReferences in syntaxNodes.Values) | ||
{ | ||
var firstPosition = identifierReferences.Select(id => id.SpanStart).Min(); | ||
var identifier = identifierReferences.First(id => id.SpanStart == firstPosition); | ||
context.ReportDiagnostic(Diagnostic.Create(rule, identifier.GetLocation(), identifier.Identifier.Text)); | ||
} | ||
return syntaxNodes.Values | ||
.Select(identifierReferences => identifierReferences.OrderBy(id => id.SpanStart).FirstOrDefault()) | ||
.Where(identifierSyntax => identifierSyntax != null); | ||
} | ||
|
||
private static bool IsFieldRelevant(IFieldSymbol fieldSymbol, ImmutableHashSet<IFieldSymbol> fieldsOfClass) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Good idea. We should do it for all other methods in this class.
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.
@michalb-sonar Yes I think so too but this refactoring doesn't belong to this PR that's why I haven't done it yet.