|
| 1 | +using Microsoft.CodeAnalysis; |
| 2 | +using Microsoft.CodeAnalysis.CSharp; |
| 3 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 4 | +using Microsoft.CodeAnalysis.Diagnostics; |
| 5 | +using System.Collections.Immutable; |
| 6 | +using System.Linq; |
| 7 | + |
| 8 | +namespace FluentAssertions.Analyzers.Tips |
| 9 | +{ |
| 10 | + [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| 11 | + public class NullConditionalAssertionAnalyzer : DiagnosticAnalyzer |
| 12 | + { |
| 13 | + public const string DiagnosticId = Constants.CodeSmell.NullConditionalAssertion; |
| 14 | + public const string Title = "Code Smell"; |
| 15 | + public const string Message = "The assertions might not be executed when using ?.Should()"; |
| 16 | + |
| 17 | + public static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticId, Title, Message, Constants.CodeSmell.Category, DiagnosticSeverity.Warning, true); |
| 18 | + |
| 19 | + public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule); |
| 20 | + |
| 21 | + public sealed override void Initialize(AnalysisContext context) |
| 22 | + { |
| 23 | + context.EnableConcurrentExecution(); |
| 24 | + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| 25 | + context.RegisterCodeBlockAction(AnalyzeCodeBlock); |
| 26 | + } |
| 27 | + |
| 28 | + private void AnalyzeCodeBlock(CodeBlockAnalysisContext context) |
| 29 | + { |
| 30 | + var method = context.CodeBlock as MethodDeclarationSyntax; |
| 31 | + if (method == null) return; |
| 32 | + |
| 33 | + if (method.Body != null) |
| 34 | + { |
| 35 | + foreach (var statement in method.Body.Statements.OfType<ExpressionStatementSyntax>()) |
| 36 | + { |
| 37 | + var diagnostic = AnalyzeExpression(statement.Expression); |
| 38 | + if (diagnostic != null) |
| 39 | + { |
| 40 | + context.ReportDiagnostic(diagnostic); |
| 41 | + } |
| 42 | + } |
| 43 | + return; |
| 44 | + } |
| 45 | + if (method.ExpressionBody != null) |
| 46 | + { |
| 47 | + var diagnostic = AnalyzeExpression(method.ExpressionBody.Expression); |
| 48 | + if (diagnostic != null) |
| 49 | + { |
| 50 | + context.ReportDiagnostic(diagnostic); |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + protected virtual Diagnostic AnalyzeExpression(ExpressionSyntax expression) |
| 56 | + { |
| 57 | + var visitor = new ConditionalAccessExpressionVisitor(); |
| 58 | + expression.Accept(visitor); |
| 59 | + |
| 60 | + return visitor.CodeSmells ? Diagnostic.Create(descriptor: Rule, location: expression.GetLocation()) : null; |
| 61 | + } |
| 62 | + |
| 63 | + private class ConditionalAccessExpressionVisitor : CSharpSyntaxWalker |
| 64 | + { |
| 65 | + public bool CodeSmells { get; private set; } |
| 66 | + public Location ConditionalAccess { get; private set; } |
| 67 | + |
| 68 | + public override void VisitConditionalAccessExpression(ConditionalAccessExpressionSyntax node) |
| 69 | + { |
| 70 | + if (CodeSmells) return; |
| 71 | + |
| 72 | + CodeSmells = node.WhenNotNull is InvocationExpressionSyntax invocation |
| 73 | + && invocation.Expression is MemberAccessExpressionSyntax memberAccess && memberAccess.IsKind(SyntaxKind.SimpleMemberAccessExpression) |
| 74 | + && memberAccess.Expression is InvocationExpressionSyntax shouldInvocation |
| 75 | + && shouldInvocation.Expression is MemberBindingExpressionSyntax memberBinding |
| 76 | + && memberBinding.Name.Identifier.ValueText == "Should"; |
| 77 | + if (CodeSmells) |
| 78 | + { |
| 79 | + ConditionalAccess = node.GetLocation(); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments