diff --git a/src/Directory.Build.props b/src/Directory.Build.props index afb564c..a70ab86 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -29,7 +29,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/src/StaticMemberAnalyzer.Analysis/Analyzers/DisposableAnalyzer.cs b/src/StaticMemberAnalyzer.Analysis/Analyzers/DisposableAnalyzer.cs index 06c48da..f7836de 100644 --- a/src/StaticMemberAnalyzer.Analysis/Analyzers/DisposableAnalyzer.cs +++ b/src/StaticMemberAnalyzer.Analysis/Analyzers/DisposableAnalyzer.cs @@ -206,6 +206,7 @@ INamedTypeSymbol disposableSymbol if (!disposableSymbol.IsRefLikeType) { + // Task or Task in System.Threading.Tasks if (disposableSymbol.Name.StartsWith(nameof(Task), StringComparison.Ordinal) && disposableSymbol.ContainingNamespace.Name == nameof(System.Threading.Tasks) && disposableSymbol.ContainingNamespace.ContainingNamespace.Name == nameof(System.Threading) @@ -272,7 +273,7 @@ INamedTypeSymbol disposableSymbol private static bool IsTypeIgnoredByAssemblyAttribute(OperationAnalysisContext context, INamedTypeSymbol disposableSymbol) { - const string ATTR_NAME = nameof(DisposableAnalyzer); + const string ATTR_NAME = "DisposableAnalyzer"; foreach (var attr in context.Compilation.Assembly.GetAttributes()) { @@ -365,7 +366,7 @@ or IDefaultValueOperation } - var memberRefOrInvokeOp = Core.UnpackNullCoalesceOperation(op); + var memberRefOrInvokeOp = Core.UnwrapNullCoalesceOperation(op); // member reference!! // --> disposable.Property; @@ -381,7 +382,7 @@ or IDefaultValueOperation } else { - var parentOp = Core.UnpackNullCoalesceOperation(memberRefOrInvokeOp.Parent); + var parentOp = Core.UnwrapNullCoalesceOperation(memberRefOrInvokeOp.Parent); if (parentOp != null) { if (parentOp is IMemberReferenceOperation or ILocalReferenceOperation) @@ -451,7 +452,7 @@ or IDefaultValueOperation // NOTE: remove parenthesizes and null warning suppressor!! // --> (((new Disposable()))) --> new Disposable() // --> (new Disposable())! --> new Disposable() - syntax = Core.UnpackParenthesizeAndNullCoalesceNodes(syntax); + syntax = Core.UnwrapParenthesizeAndNullSuppressorNodes(syntax); // return statement? @@ -529,7 +530,7 @@ or IDefaultValueOperation } } // --> if (disposable == ...) - // --> switch (disposable) ... + // --> while (disposable == ...) else if (op.Parent is IBinaryOperation) { // don't allow creation operation pass the warning diff --git a/src/StaticMemberAnalyzer.Analysis/Analyzers/UnderliningAnalyzer.cs b/src/StaticMemberAnalyzer.Analysis/Analyzers/UnderliningAnalyzer.cs index 40fcf10..215c6b7 100644 --- a/src/StaticMemberAnalyzer.Analysis/Analyzers/UnderliningAnalyzer.cs +++ b/src/StaticMemberAnalyzer.Analysis/Analyzers/UnderliningAnalyzer.cs @@ -1,17 +1,10 @@ -/* Core ================================================================ */ -#define STMG_DEBUG_MESSAGE // some try-catch will be enabled +#define STMG_DEBUG_MESSAGE #if DEBUG == false #undef STMG_DEBUG_MESSAGE #endif -#if STMG_DEBUG_MESSAGE -//#define STMG_DEBUG_MESSAGE_VERBOSE // for debugging. many of additional debug diagnostics will be emitted -#endif -/* /Core ================================================================ */ - #define STMG_USE_ATTRIBUTE_CACHE #define STMG_USE_DESCRIPTION_CACHE -//#define STMG_ENABLE_LINE_FILL using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; @@ -206,23 +199,12 @@ public override void Initialize(AnalysisContext context) //https://github.com/dotnet/roslyn/blob/main/docs/analyzers/Analyzer%20Actions%20Semantics.md - /* = symbol = */ - context.RegisterSymbolAction(DrawUnderlineOnSymbols, cache_descriptionTargetSymbols); - - /* = syntax = */ - context.RegisterSyntaxNodeAction(DrawUnderlineOnSyntaxNodes, cache_descriptionTargetSyntaxes); - - /* = operation = */ - context.RegisterOperationAction(DrawUnderlineOnOperators, cache_descriptionTargetOperations); - - /* = cache = */ - context.RegisterCompilationStartAction(InitializeAndRegisterCallbacks); } @@ -323,14 +305,6 @@ private static void DrawUnderlineOnSymbols(SymbolAnalysisContext context) var symbolToDescription = (ts_symbolToDescription ??= new()); var descAttrToMessage = (ts_descAttrToMessage ??= new()); -#if STMG_DEBUG_MESSAGE_VERBOSE - Core.ReportDebugMessage(context.ReportDiagnostic, - "DEBUG SYMBOL", - GetAndUpdateDescriptionCache(symbol, symbolToDescription, /*filePathToModel,*/ descAttrToMessage, compilation, token) - ?? "=== NO DESCRIPTION === " + symbol.Name, - symbol.Locations); -#endif - Underlining(symbol.Locations, symbol, Rule_DeclarationDesc, reportAction, symbolToDescription, /*filePathToModel,*/ descAttrToMessage, compilation, token, 0); @@ -407,13 +381,6 @@ private static void DrawUnderlineOnSyntaxNodes(SyntaxNodeAnalysisContext context goto RERUN_SIMPLE_BASE_TYPE; } -#if STMG_DEBUG_MESSAGE_VERBOSE - Core.ReportDebugMessage(context.ReportDiagnostic, - "DEBUG NODE (NOT FOUND)", - "=== SYMBOL NOT FOUND ===", - singleLocation); -#endif - symbol = symbolCandidate.CandidateSymbols.FirstOrDefault(); if (symbol == null) return; @@ -425,14 +392,6 @@ private static void DrawUnderlineOnSyntaxNodes(SyntaxNodeAnalysisContext context } else if (symbol is IParameterSymbol paramSymbol) { -#if STMG_DEBUG_MESSAGE_VERBOSE - Core.ReportDebugMessage(context.ReportDiagnostic, - "DEBUG NODE (PARAM)", - GetAndUpdateDescriptionCache(symbol, symbolToDescription, /*filePathToModel,*/ descAttrToMessage, compilation, token) - ?? "=== NO DESCRIPTION === " + symbol.Name, - singleLocation); -#endif - // NOTE: draw underline on methodParameter --> void Method([DescriptionAttribute] T methodParameter); Underlining(singleLocation, symbol, rule, reportAction, symbolToDescription, /*filePathToModel,*/ descAttrToMessage, compilation, token, @@ -444,14 +403,6 @@ private static void DrawUnderlineOnSyntaxNodes(SyntaxNodeAnalysisContext context } -#if STMG_DEBUG_MESSAGE_VERBOSE - Core.ReportDebugMessage(context.ReportDiagnostic, - "DEBUG NODE: " + syntax.Kind(), - GetAndUpdateDescriptionCache(symbol, symbolToDescription, /*filePathToModel,*/ descAttrToMessage, compilation, token) - ?? "=== NO DESCRIPTION === " + symbol.Name, - singleLocation); -#endif - if (symbol.IsImplicitlyDeclared) return; @@ -504,10 +455,6 @@ CancellationToken token if (lambdaType == null) { -#if STMG_DEBUG_MESSAGE_VERBOSE - Core.ReportDebugMessage(reportAction, "LAMBDA PARAM TYPE NOT FOUND", syntax.Kind().ToString(), syntax.GetLocation()); -#endif - return; } @@ -757,21 +704,10 @@ int dotTokenCount if (description == null) { -#if STMG_DEBUG_MESSAGE_VERBOSE - Core.ReportDebugMessage(reportAction, - targetSymbol.GetType().Name, - "=== NO DESCRIPTION === " + targetSymbol, - locations); -#endif - goto NEXT; } -#if STMG_DEBUG_MESSAGE_VERBOSE - description += $" cache:{new Random().Next()}"; -#endif - var args = GetMessageFormatArgs(targetSymbol, description); foreach (var location in locations) { @@ -1121,10 +1057,6 @@ CancellationToken token description = "Take care to use (no details provided)"; //SpanConcat(span, " has Description attribute w/o args".AsSpan()); } -#if STMG_DEBUG_MESSAGE_VERBOSE - description += $" (from:{symbol.Kind})"; -#endif - #if STMG_USE_DESCRIPTION_CACHE // NOTE: existence checked is done in caller!! @@ -1195,17 +1127,6 @@ object[] messageArgs Rule_LineLeadingDesc, tree.GetLocation(new(start, length)), messageArgs)); } } - -#if STMG_ENABLE_LINE_FILL - start += offset; - int linefillEnd = locStart - 1; - - // add line fill - if (start < linefillEnd) - { - __DrawUnderlinePerChar(reportAction, tree, start, linefillEnd, Rule_LineFillDesc, messageArgs); - } -#endif } //lineEnd!! diff --git a/src/StaticMemberAnalyzer.Analysis/Core.cs b/src/StaticMemberAnalyzer.Analysis/Core.cs index fb8988a..3537ffd 100644 --- a/src/StaticMemberAnalyzer.Analysis/Core.cs +++ b/src/StaticMemberAnalyzer.Analysis/Core.cs @@ -126,14 +126,14 @@ internal static void ReportDebugMessage(Action reportMethod, IOperat [CallerLineNumber] int lineNumber = -1 ) { - op = UnpackNullCoalesceOperation(op); + op = UnwrapNullCoalesceOperation(op); ReportDebugMessage(reportMethod, $"{callerMember}\n#{lineNumber}", ImmutableArray.Create(location), $"Op: {op.Kind} ({op.Type?.Name})", - $"Parent: {op.Parent?.UnpackNullCoalesceOperation().Kind} ({op.Parent?.Type?.Name})", - $"Grand Parent: {op.Parent?.Parent?.UnpackNullCoalesceOperation().Kind} ({op.Parent?.Parent?.Type?.Name})", + $"Parent: {op.Parent?.UnwrapNullCoalesceOperation().Kind} ({op.Parent?.Type?.Name})", + $"Grand Parent: {op.Parent?.Parent?.UnwrapNullCoalesceOperation().Kind} ({op.Parent?.Parent?.Type?.Name})", "> " + new string(op.Syntax?.ToString().Take(72).ToArray()), - $"Child: {op.Children?.FirstOrDefault()?.UnpackNullCoalesceOperation().Kind} ({op.Children?.FirstOrDefault().Type?.Name})" + $"Child: {op.Children?.FirstOrDefault()?.UnwrapNullCoalesceOperation().Kind} ({op.Children?.FirstOrDefault()?.Type?.Name})" ); } @@ -214,7 +214,7 @@ internal static void ReportDebugMessage(Action reportMethod, stri /* node & operation ================================================================ */ - internal static SyntaxNode UnpackParenthesizeAndNullCoalesceNodes(this SyntaxNode syntax) + internal static SyntaxNode UnwrapParenthesizeAndNullSuppressorNodes(this SyntaxNode syntax) { while (syntax.Parent is ParenthesizedExpressionSyntax || syntax.Parent.IsKind(SyntaxKind.SuppressNullableWarningExpression)) { @@ -224,7 +224,7 @@ internal static SyntaxNode UnpackParenthesizeAndNullCoalesceNodes(this SyntaxNod } - internal static IOperation UnpackNullCoalesceOperation(this IOperation op) + internal static IOperation UnwrapNullCoalesceOperation(this IOperation op) { return (op as IConditionalAccessOperation)?.Operation ?? op; }