|
| 1 | +namespace OpenStackNetAnalyzers |
| 2 | +{ |
| 3 | + using System; |
| 4 | + using System.Collections.Immutable; |
| 5 | + using Microsoft.CodeAnalysis; |
| 6 | + using Microsoft.CodeAnalysis.CSharp; |
| 7 | + using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 8 | + using Microsoft.CodeAnalysis.Diagnostics; |
| 9 | + |
| 10 | + [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| 11 | + public class AssertNullAnalyzer : DiagnosticAnalyzer |
| 12 | + { |
| 13 | + public const string DiagnosticId = "AssertNull"; |
| 14 | + internal const string Title = "Assert.IsNull and Assert.IsNotNull should only be used with nullable types"; |
| 15 | + internal const string MessageFormat = "'Assert.{0}' should not be used with the value type '{1}'"; |
| 16 | + internal const string Category = "OpenStack.Maintainability"; |
| 17 | + internal const string Description = "Assert.IsNull and Assert.IsNotNull should only be used with nullable types"; |
| 18 | + |
| 19 | + private static DiagnosticDescriptor Descriptor = |
| 20 | + new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Error, isEnabledByDefault: true, description: Description); |
| 21 | + |
| 22 | + private static readonly ImmutableArray<DiagnosticDescriptor> _supportedDiagnostics = |
| 23 | + ImmutableArray.Create(Descriptor); |
| 24 | + |
| 25 | + public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics |
| 26 | + { |
| 27 | + get |
| 28 | + { |
| 29 | + return _supportedDiagnostics; |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + public override void Initialize(AnalysisContext context) |
| 34 | + { |
| 35 | + context.RegisterSyntaxNodeAction(HandleInvocationExpression, SyntaxKind.InvocationExpression); |
| 36 | + } |
| 37 | + |
| 38 | + private void HandleInvocationExpression(SyntaxNodeAnalysisContext context) |
| 39 | + { |
| 40 | + InvocationExpressionSyntax syntax = (InvocationExpressionSyntax)context.Node; |
| 41 | + if (syntax.Expression == null || !(syntax.ArgumentList?.Arguments.Count > 0)) |
| 42 | + return; |
| 43 | + |
| 44 | + SymbolInfo symbolInfo = context.SemanticModel.GetSymbolInfo(syntax.Expression, context.CancellationToken); |
| 45 | + IMethodSymbol methodSymbol = symbolInfo.Symbol as IMethodSymbol; |
| 46 | + if (methodSymbol == null) |
| 47 | + return; |
| 48 | + |
| 49 | + if (!string.Equals("IsNotNull", methodSymbol.Name, StringComparison.Ordinal) |
| 50 | + && !string.Equals("IsNull", methodSymbol.Name, StringComparison.Ordinal)) |
| 51 | + { |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + var containingType = methodSymbol.ContainingType; |
| 56 | + if (!string.Equals("Assert", containingType?.Name, StringComparison.Ordinal)) |
| 57 | + return; |
| 58 | + |
| 59 | + if (syntax.ArgumentList.Arguments[0].NameColon != null) |
| 60 | + return; |
| 61 | + |
| 62 | + ExpressionSyntax argumentExpression = syntax.ArgumentList.Arguments[0]?.Expression; |
| 63 | + if (argumentExpression == null) |
| 64 | + return; |
| 65 | + |
| 66 | + TypeInfo typeInfo = context.SemanticModel.GetTypeInfo(argumentExpression); |
| 67 | + INamedTypeSymbol namedType = typeInfo.Type as INamedTypeSymbol; |
| 68 | + if (namedType == null) |
| 69 | + return; |
| 70 | + |
| 71 | + if (!namedType.IsValueType) |
| 72 | + { |
| 73 | + // don't report the diagnostic for reference types |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + INamedTypeSymbol originalDefinition = namedType.OriginalDefinition; |
| 78 | + if (originalDefinition == null |
| 79 | + || originalDefinition.SpecialType == SpecialType.System_Nullable_T |
| 80 | + || originalDefinition.SpecialType == SpecialType.System_ValueType |
| 81 | + || originalDefinition.SpecialType == SpecialType.System_Enum) |
| 82 | + { |
| 83 | + // don't report the diagnostic for "special" and nullable value types |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + string typeName = namedType.ToMinimalDisplayString(context.SemanticModel, argumentExpression.SpanStart, SymbolDisplayFormat.CSharpErrorMessageFormat); |
| 88 | + context.ReportDiagnostic(Diagnostic.Create(Descriptor, syntax.GetLocation(), methodSymbol.Name, typeName)); |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments