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

Replace deprecated ReportIssue in more locations #9423

Merged
merged 10 commits into from
Jun 14, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ protected override void Initialize(SonarAnalysisContext context) =>
{
var typeDeclaration = (BaseTypeDeclarationSyntax)c.Node;
var implementedTypes = typeDeclaration.BaseList?.Types;
if (implementedTypes == null || c.IsRedundantPositionalRecordContext())
if (implementedTypes is null || c.IsRedundantPositionalRecordContext())
{
return;
}

List<Diagnostic> issues = null;
List<Issue> issues = null;
var containingType = (INamedTypeSymbol)c.ContainingSymbol;
foreach (var typeSymbol in containingType.Interfaces.Concat(new[] { containingType.BaseType }).WhereNotNull())
foreach (var typeSymbol in containingType.Interfaces.Concat([containingType.BaseType]).WhereNotNull())
{
if (typeSymbol.OriginalDefinition.IsAny(GenericTypes))
{
Expand All @@ -65,12 +65,12 @@ protected override void Initialize(SonarAnalysisContext context) =>

if (SuggestGenericCollectionType(typeSymbol) is { } suggestedGenericType)
{
issues ??= new();
issues.Add(Diagnostic.Create(Rule, typeDeclaration.Identifier.GetLocation(), suggestedGenericType));
issues ??= [];
issues.Add(new(typeDeclaration.Identifier, suggestedGenericType));
}
}

issues?.ForEach(d => c.ReportIssue(d));
issues?.ForEach(x => c.ReportIssue(Rule, x.Identifier, x.SuggestedGenericType));
pavel-mikula-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
},
SyntaxKind.ClassDeclaration,
SyntaxKind.StructDeclaration,
Expand All @@ -79,5 +79,7 @@ protected override void Initialize(SonarAnalysisContext context) =>

private static string SuggestGenericCollectionType(ITypeSymbol typeSymbol) =>
NonGenericToGenericMapping.FirstOrDefault(pair => pair.Key.Matches(typeSymbol)).Value;

private readonly record struct Issue(SyntaxToken Identifier, string SuggestedGenericType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,16 @@ protected override void Initialize(SonarAnalysisContext context) =>
}

var issueFinder = new IssueFinder(classSymbol, c.SemanticModel);
foreach (var diagnostic in classDeclaration.Members.Select(issueFinder.FindIssue).WhereNotNull())
foreach (var issue in classDeclaration.Members.Select(issueFinder.FindIssue).WhereNotNull())
{
c.ReportIssue(diagnostic);
c.ReportIssue(Rule, issue.Location, issue.Symbol.ToDisplayString());
}
},
SyntaxKind.ClassDeclaration,
SyntaxKindEx.RecordClassDeclaration);

private record struct Issue(Location Location, ISymbol Symbol);
pavel-mikula-sonarsource marked this conversation as resolved.
Show resolved Hide resolved

private sealed class IssueFinder
{
private readonly IList<IMethodSymbol> allBaseClassMethods;
Expand All @@ -68,19 +70,15 @@ public IssueFinder(ITypeSymbol classSymbol, SemanticModel semanticModel)
allBaseClassProperties = allBaseClassMembers.OfType<IPropertySymbol>().ToList();
}

public Diagnostic FindIssue(MemberDeclarationSyntax memberDeclaration)
{
var memberSymbol = semanticModel.GetDeclaredSymbol(memberDeclaration);

if (memberSymbol is IMethodSymbol methodSymbol)
public Issue? FindIssue(MemberDeclarationSyntax memberDeclaration) =>
semanticModel.GetDeclaredSymbol(memberDeclaration) switch
{
return FindMethodIssue(memberDeclaration, methodSymbol);
}

return memberSymbol is IPropertySymbol propertySymbol ? FindPropertyIssue(memberDeclaration, propertySymbol) : null;
}
IMethodSymbol methodSymbol => FindMethodIssue(memberDeclaration, methodSymbol),
IPropertySymbol propertySymbol => FindPropertyIssue(memberDeclaration, propertySymbol),
_ => null,
};

private Diagnostic FindMethodIssue(MemberDeclarationSyntax memberDeclaration, IMethodSymbol methodSymbol)
private Issue? FindMethodIssue(MemberDeclarationSyntax memberDeclaration, IMethodSymbol methodSymbol)
{
if (memberDeclaration is not MethodDeclarationSyntax methodDeclaration
|| methodDeclaration.Modifiers.Any(SyntaxKind.NewKeyword))
Expand All @@ -96,14 +94,14 @@ private Diagnostic FindMethodIssue(MemberDeclarationSyntax memberDeclaration, IM
var location = methodDeclaration.Identifier.GetLocation();
if (location != null)
{
return Diagnostic.Create(Rule, location, hidingMethod);
return new(location, hidingMethod);
}
}

return null;
}

private Diagnostic FindPropertyIssue(MemberDeclarationSyntax memberDeclaration, IPropertySymbol propertySymbol)
private Issue? FindPropertyIssue(MemberDeclarationSyntax memberDeclaration, IPropertySymbol propertySymbol)
{
if (memberDeclaration is not PropertyDeclarationSyntax propertyDeclaration
|| propertyDeclaration.Modifiers.Any(SyntaxKind.NewKeyword))
Expand All @@ -115,7 +113,7 @@ private Diagnostic FindPropertyIssue(MemberDeclarationSyntax memberDeclaration,
if (hidingProperty != null)
{
var location = propertyDeclaration.Identifier.GetLocation();
return Diagnostic.Create(Rule, location, hidingProperty);
return new(location, hidingProperty);
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,16 @@ protected override void Initialize(SonarAnalysisContext context) =>
}

var issueFinder = new IssueFinder(declaredSymbol, c.SemanticModel);
foreach (var diagnostic in declarationSyntax.Members.Select(issueFinder.FindIssue).WhereNotNull())
foreach (var issue in declarationSyntax.Members.Select(issueFinder.FindIssue).WhereNotNull())
{
c.ReportIssue(diagnostic);
c.ReportIssue(Rule, issue.Location, issue.HiddenBaseMethod.ToDisplayString());
}
},
SyntaxKind.ClassDeclaration,
SyntaxKindEx.RecordClassDeclaration);

private readonly record struct Issue(Location Location, IMethodSymbol HiddenBaseMethod);
pavel-mikula-sonarsource marked this conversation as resolved.
Show resolved Hide resolved

private sealed class IssueFinder
{
private enum Match
Expand All @@ -67,17 +69,17 @@ public IssueFinder(ITypeSymbol typeSymbol, SemanticModel semanticModel)
allBaseTypeMethods = GetAllBaseMethods(typeSymbol);
}

public Diagnostic FindIssue(MemberDeclarationSyntax memberDeclaration)
public Issue? FindIssue(MemberDeclarationSyntax memberDeclaration)
{
var issueLocation = (memberDeclaration as MethodDeclarationSyntax)?.Identifier.GetLocation();

if (semanticModel.GetDeclaredSymbol(memberDeclaration) is not IMethodSymbol methodSymbol || issueLocation == null)
if (issueLocation is null || semanticModel.GetDeclaredSymbol(memberDeclaration) is not IMethodSymbol methodSymbol)
{
return null;
}

var baseMethodHidden = FindBaseMethodHiddenByMethod(methodSymbol);
return baseMethodHidden != null ? Diagnostic.Create(Rule, issueLocation, baseMethodHidden) : null;
return baseMethodHidden is not null ? new(issueLocation, baseMethodHidden) : null;
}

private static List<IMethodSymbol> GetAllBaseMethods(ITypeSymbol typeSymbol) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,16 @@ protected override void Initialize(SonarAnalysisContext context) =>
}
linesWithShiftOperations.Add(shift.Line);
if (shift.Diagnostic != null)
if (shift.Node is not null)
{
c.ReportIssue(shift.Diagnostic);
c.ReportIssue(Rule, shift.Node, shift.Description);
}
}
zeroShiftIssues
.Where(sh => !ContainsShiftExpressionWithinTwoLines(linesWithShiftOperations, sh.Line))
.ToList()
.ForEach(sh => c.ReportIssue(sh.Diagnostic));
foreach (var shift in zeroShiftIssues.Where(x => !ContainsShiftExpressionWithinTwoLines(linesWithShiftOperations, x.Line)))
{
c.ReportIssue(Rule, shift.Node, shift.Description);
}
},
SyntaxKind.MethodDeclaration,
SyntaxKind.PropertyDeclaration);
Expand Down Expand Up @@ -196,18 +196,20 @@ private static string FindProblemDescription(int typeSizeInBits, int shiftBy, Sh

private sealed class ShiftInstance
{
public Diagnostic Diagnostic { get; }
public string Description { get; }
public SyntaxNode Node { get; }
public bool IsLiteralZero { get; }
public int Line { get; }

public ShiftInstance(SyntaxNode node) =>
Line = node.GetLineNumberToReport();

public ShiftInstance(string description, bool isLieralZero, SyntaxNode node)
public ShiftInstance(string description, bool isLiteralZero, SyntaxNode node)
: this(node)
{
Diagnostic = Diagnostic.Create(Rule, node.GetLocation(), description);
IsLiteralZero = isLieralZero;
Description = description;
IsLiteralZero = isLiteralZero;
Node = node;
}
}
}
Expand Down
Loading