Skip to content

Commit

Permalink
Apply the same refactoring we did for codefixes (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
sailro authored Feb 10, 2021
1 parent a5ef9b5 commit d120ad2
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 29 deletions.
4 changes: 1 addition & 3 deletions src/Microsoft.Unity.Analyzers/ContextMenuSuppressor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,10 @@ public override void ReportSuppressions(SuppressionAnalysisContext context)
private void AnalyzeDiagnostic(Diagnostic diagnostic, SuppressionAnalysisContext context)
{
var location = diagnostic.Location;
var sourceTree = location.SourceTree;
var root = sourceTree.GetRoot(context.CancellationToken);
var node = root.FindNode(location.SourceSpan);
var model = context.GetSemanticModel(location.SourceTree);
var symbols = new List<ISymbol>();

var node = context.GetSuppressibleNode<SyntaxNode>(diagnostic, n => n is MethodDeclarationSyntax || n is VariableDeclaratorSyntax || n is FieldDeclarationSyntax);
switch (node)
{
case MethodDeclarationSyntax method:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.Unity.Analyzers.Resources;

Expand All @@ -28,13 +29,11 @@ public override void ReportSuppressions(SuppressionAnalysisContext context)

private static void AnalyzeDiagnostic(Diagnostic diagnostic, SuppressionAnalysisContext context)
{
var sourceTree = diagnostic.Location.SourceTree;
var root = sourceTree.GetRoot(context.CancellationToken);
var node = root.FindNode(diagnostic.Location.SourceSpan);
var model = context.GetSemanticModel(diagnostic.Location.SourceTree);
var methodDeclarationSyntax = context.GetSuppressibleNode<MethodDeclarationSyntax>(diagnostic);

// Reuse the same detection logic regarding decorated methods with *InitializeOnLoadMethodAttribute
if (InitializeOnLoadMethodAnalyzer.MethodMatches(node, model, out _, out _))
if (InitializeOnLoadMethodAnalyzer.MethodMatches(methodDeclarationSyntax, model, out _, out _))
context.ReportSuppression(Suppression.Create(Rule, diagnostic));
}
}
Expand Down
13 changes: 9 additions & 4 deletions src/Microsoft.Unity.Analyzers/MessageSuppressor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,21 @@ public override void ReportSuppressions(SuppressionAnalysisContext context)

private void AnalyzeDiagnostic(Diagnostic diagnostic, SuppressionAnalysisContext context)
{
var node = diagnostic.Location.SourceTree.GetRoot(context.CancellationToken).FindNode(diagnostic.Location.SourceSpan);
var node = context.GetSuppressibleNode<SyntaxNode>(diagnostic, n => n is ParameterSyntax || n is MethodDeclarationSyntax);

if (node is ParameterSyntax)
node = node.Ancestors().OfType<MethodDeclarationSyntax>().FirstOrDefault();
{
node = node
.Ancestors()
.OfType<MethodDeclarationSyntax>()
.FirstOrDefault();
}

if (!(node is MethodDeclarationSyntax method))
if (node == null)
return;

var model = context.GetSemanticModel(diagnostic.Location.SourceTree);
if (!(model.GetDeclaredSymbol(method) is IMethodSymbol methodSymbol))
if (!(model.GetDeclaredSymbol(node) is IMethodSymbol methodSymbol))
return;

var scriptInfo = new ScriptInfo(methodSymbol.ContainingType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ public override void ReportSuppressions(SuppressionAnalysisContext context)
foreach (var diagnostic in context.ReportedDiagnostics)
{
var root = diagnostic.Location.SourceTree.GetRoot();
var location = root?.FindNode(diagnostic.Location.SourceSpan);
var node = root?.FindNode(diagnostic.Location.SourceSpan);

var classDeclaration = location?.FirstAncestorOrSelf<ClassDeclarationSyntax>();
var classDeclaration = node?.FirstAncestorOrSelf<ClassDeclarationSyntax>();
if (classDeclaration is null)
continue;

Expand All @@ -44,7 +44,7 @@ public override void ReportSuppressions(SuppressionAnalysisContext context)
if (!scriptInfo.HasMessages)
continue;

var propertyDeclaration = location.FirstAncestorOrSelf<PropertyDeclarationSyntax>();
var propertyDeclaration = node.FirstAncestorOrSelf<PropertyDeclarationSyntax>();

//handle properties before fields to minimize double checking of potential backing fields
if (!(propertyDeclaration is null))
Expand All @@ -53,7 +53,8 @@ public override void ReportSuppressions(SuppressionAnalysisContext context)
continue;
}

if (location is VariableDeclaratorSyntax fieldDeclaration)
var fieldDeclaration = context.GetSuppressibleNode<VariableDeclaratorSyntax>(diagnostic);
if (fieldDeclaration != null)
AnalyzeFields(fieldDeclaration, diagnostic, context, root);

//TODO handle nullable warnings for constructors => diagnostic location is now on constructor
Expand Down
7 changes: 4 additions & 3 deletions src/Microsoft.Unity.Analyzers/SerializeFieldSuppressor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.Unity.Analyzers.Resources;
using UnityEngine;
Expand Down Expand Up @@ -58,12 +59,12 @@ private static bool IsSuppressable(IFieldSymbol fieldSymbol)

private void AnalyzeDiagnostic(Diagnostic diagnostic, SuppressionAnalysisContext context)
{
var node = diagnostic.Location.SourceTree.GetRoot(context.CancellationToken).FindNode(diagnostic.Location.SourceSpan);
if (node == null)
var fieldDeclarationSyntax = context.GetSuppressibleNode<VariableDeclaratorSyntax>(diagnostic);
if (fieldDeclarationSyntax == null)
return;

var model = context.GetSemanticModel(diagnostic.Location.SourceTree);
if (!(model.GetDeclaredSymbol(node) is IFieldSymbol fieldSymbol))
if (!(model.GetDeclaredSymbol(fieldDeclarationSyntax) is IFieldSymbol fieldSymbol))
return;

if (!IsSuppressable(fieldSymbol))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*--------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See LICENSE in the project root for license information.
*-------------------------------------------------------------------------------------------*/

using System;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;

namespace Microsoft.Unity.Analyzers
{
internal static class SuppressionAnalysisContextExtensions
{
public static T GetSuppressibleNode<T>(this SuppressionAnalysisContext context, Diagnostic diagnostic) where T : SyntaxNode
{
return GetSuppressibleNode<T>(context, diagnostic, n => true);
}

public static T GetSuppressibleNode<T>(this SuppressionAnalysisContext context, Diagnostic diagnostic, Func<T, bool> predicate) where T : SyntaxNode
{
var location = diagnostic.Location;
var sourceTree = location.SourceTree;
var root = sourceTree.GetRoot(context.CancellationToken);

return root?
.FindNode(location.SourceSpan)
.DescendantNodesAndSelf()
.OfType<T>()
.FirstOrDefault(predicate);
}
}
}
10 changes: 2 additions & 8 deletions src/Microsoft.Unity.Analyzers/UnityObjectNullHandling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,8 @@ public override void ReportSuppressions(SuppressionAnalysisContext context)

private void AnalyzeDiagnostic(Diagnostic diagnostic, SuppressionAnalysisContext context)
{
var root = diagnostic.Location.SourceTree.GetRoot(context.CancellationToken);
var node = root?.FindNode(diagnostic.Location.SourceSpan);

// We can be tricked by extra parentheses for the condition, so go to the first concrete binary expression
if (!(node?
.DescendantNodesAndSelf()
.OfType<BinaryExpressionSyntax>()
.FirstOrDefault() is BinaryExpressionSyntax binaryExpression))
var binaryExpression = context.GetSuppressibleNode<BinaryExpressionSyntax>(diagnostic);
if (binaryExpression == null)
return;

AnalyzeBinaryExpression(diagnostic, context, binaryExpression);
Expand Down
6 changes: 3 additions & 3 deletions src/Microsoft.Unity.Analyzers/UnusedMethodSuppressor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ private static void AnalyzeDiagnostic(Diagnostic diagnostic, SuppressionAnalysis
{
var sourceTree = diagnostic.Location.SourceTree;
var root = sourceTree.GetRoot(context.CancellationToken);
var node = root.FindNode(diagnostic.Location.SourceSpan);

if (!(node is MethodDeclarationSyntax method))
var methodDeclarationSyntax = context.GetSuppressibleNode<MethodDeclarationSyntax>(diagnostic);
if (methodDeclarationSyntax == null)
return;

var model = context.GetSemanticModel(diagnostic.Location.SourceTree);
if (!(model.GetDeclaredSymbol(method) is IMethodSymbol methodSymbol))
if (!(model.GetDeclaredSymbol(methodDeclarationSyntax) is IMethodSymbol methodSymbol))
return;

var typeSymbol = methodSymbol.ContainingType;
Expand Down

0 comments on commit d120ad2

Please sign in to comment.