From 05073cde89bdcf457346b0c516272b10b7df6275 Mon Sep 17 00:00:00 2001 From: Manish Vasani Date: Thu, 3 Dec 2020 18:38:44 -0800 Subject: [PATCH 1/6] Update auto-generated documentation file --- src/NetAnalyzers/Microsoft.CodeAnalysis.NetAnalyzers.sarif | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/NetAnalyzers/Microsoft.CodeAnalysis.NetAnalyzers.sarif b/src/NetAnalyzers/Microsoft.CodeAnalysis.NetAnalyzers.sarif index c41ced8d15..37d29ac62f 100644 --- a/src/NetAnalyzers/Microsoft.CodeAnalysis.NetAnalyzers.sarif +++ b/src/NetAnalyzers/Microsoft.CodeAnalysis.NetAnalyzers.sarif @@ -5,7 +5,7 @@ { "tool": { "name": "Microsoft.CodeAnalysis.CSharp.NetAnalyzers", - "version": "5.0.1", + "version": "6.0.0", "language": "en-US" }, "rules": { @@ -290,7 +290,7 @@ { "tool": { "name": "Microsoft.CodeAnalysis.NetAnalyzers", - "version": "5.0.1", + "version": "6.0.0", "language": "en-US" }, "rules": { @@ -5068,7 +5068,7 @@ { "tool": { "name": "Microsoft.CodeAnalysis.VisualBasic.NetAnalyzers", - "version": "5.0.1", + "version": "6.0.0", "language": "en-US" }, "rules": { From f83295ace79fb958991b77f83a8db642e4edc946 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaury=20Lev=C3=A9?= Date: Tue, 8 Dec 2020 15:45:01 +0100 Subject: [PATCH 2/6] Remove rule CA1801 - replaced by IDE0060 --- .../CSharpReviewUnusedParameters.Fixer.cs | 28 - .../CSharpReviewUnusedParametersAnalyzer.cs | 35 - .../ReviewUnusedParameters.Fixer.cs | 186 --- .../Maintainability/ReviewUnusedParameters.cs | 252 --- .../Microsoft.CodeAnalysis.NetAnalyzers.md | 12 - .../Microsoft.CodeAnalysis.NetAnalyzers.sarif | 40 - .../ReviewUnusedParametersTests.Fixer.cs | 896 ----------- .../ReviewUnusedParametersTests.cs | 1422 ----------------- .../BasicReviewUnusedParameters.Fixer.vb | 26 - .../BasicReviewUnusedParametersAnalyzer.vb | 20 - 10 files changed, 2917 deletions(-) delete mode 100644 src/NetAnalyzers/CSharp/Microsoft.CodeQuality.Analyzers/Maintainability/CSharpReviewUnusedParameters.Fixer.cs delete mode 100644 src/NetAnalyzers/CSharp/Microsoft.CodeQuality.Analyzers/Maintainability/CSharpReviewUnusedParametersAnalyzer.cs delete mode 100644 src/NetAnalyzers/Core/Microsoft.CodeQuality.Analyzers/Maintainability/ReviewUnusedParameters.Fixer.cs delete mode 100644 src/NetAnalyzers/Core/Microsoft.CodeQuality.Analyzers/Maintainability/ReviewUnusedParameters.cs delete mode 100644 src/NetAnalyzers/UnitTests/Microsoft.CodeQuality.Analyzers/Maintainability/ReviewUnusedParametersTests.Fixer.cs delete mode 100644 src/NetAnalyzers/UnitTests/Microsoft.CodeQuality.Analyzers/Maintainability/ReviewUnusedParametersTests.cs delete mode 100644 src/NetAnalyzers/VisualBasic/Microsoft.CodeQuality.Analyzers/Maintainability/BasicReviewUnusedParameters.Fixer.vb delete mode 100644 src/NetAnalyzers/VisualBasic/Microsoft.CodeQuality.Analyzers/Maintainability/BasicReviewUnusedParametersAnalyzer.vb diff --git a/src/NetAnalyzers/CSharp/Microsoft.CodeQuality.Analyzers/Maintainability/CSharpReviewUnusedParameters.Fixer.cs b/src/NetAnalyzers/CSharp/Microsoft.CodeQuality.Analyzers/Maintainability/CSharpReviewUnusedParameters.Fixer.cs deleted file mode 100644 index ee4361aded..0000000000 --- a/src/NetAnalyzers/CSharp/Microsoft.CodeQuality.Analyzers/Maintainability/CSharpReviewUnusedParameters.Fixer.cs +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System.Composition; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CodeFixes; -using Microsoft.CodeAnalysis.CSharp; -using Microsoft.CodeQuality.Analyzers.Maintainability; - -namespace Microsoft.CodeQuality.CSharp.Analyzers.Maintainability -{ - /// - /// CA1801: Review unused parameters - /// - [ExportCodeFixProvider(LanguageNames.CSharp), Shared] - public sealed class CSharpReviewUnusedParametersFixer : ReviewUnusedParametersFixer - { - protected override SyntaxNode GetParameterDeclarationNode(SyntaxNode node) - { - return node; - } - - protected override bool CanContinuouslyLeadToObjectCreationOrInvocation(SyntaxNode node) - { - var kind = node.Kind(); - return kind is SyntaxKind.QualifiedName or SyntaxKind.IdentifierName or SyntaxKind.SimpleMemberAccessExpression; - } - } -} \ No newline at end of file diff --git a/src/NetAnalyzers/CSharp/Microsoft.CodeQuality.Analyzers/Maintainability/CSharpReviewUnusedParametersAnalyzer.cs b/src/NetAnalyzers/CSharp/Microsoft.CodeQuality.Analyzers/Maintainability/CSharpReviewUnusedParametersAnalyzer.cs deleted file mode 100644 index 0880869ad8..0000000000 --- a/src/NetAnalyzers/CSharp/Microsoft.CodeQuality.Analyzers/Maintainability/CSharpReviewUnusedParametersAnalyzer.cs +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; -using Microsoft.CodeAnalysis.Diagnostics; -using Microsoft.CodeQuality.Analyzers.Maintainability; - -namespace Microsoft.CodeQuality.CSharp.Analyzers.Maintainability -{ - /// - /// CA1801: Review unused parameters - /// - [DiagnosticAnalyzer(LanguageNames.CSharp)] - public sealed class CSharpReviewUnusedParametersAnalyzer : ReviewUnusedParametersAnalyzer - { - private const SyntaxKind RecordDeclaration = (SyntaxKind)9063; - - protected override bool IsPositionalRecordPrimaryConstructor(IMethodSymbol methodSymbol) - { - if (methodSymbol.MethodKind != MethodKind.Constructor) - { - return false; - } - - if (methodSymbol.DeclaringSyntaxReferences.Length == 0) - { - return false; - } - - return methodSymbol.DeclaringSyntaxReferences[0] - .GetSyntax() - .IsKind(RecordDeclaration); - } - } -} diff --git a/src/NetAnalyzers/Core/Microsoft.CodeQuality.Analyzers/Maintainability/ReviewUnusedParameters.Fixer.cs b/src/NetAnalyzers/Core/Microsoft.CodeQuality.Analyzers/Maintainability/ReviewUnusedParameters.Fixer.cs deleted file mode 100644 index c7b31524fc..0000000000 --- a/src/NetAnalyzers/Core/Microsoft.CodeQuality.Analyzers/Maintainability/ReviewUnusedParameters.Fixer.cs +++ /dev/null @@ -1,186 +0,0 @@ -// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Collections.Generic; -using System.Collections.Immutable; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; -using Analyzer.Utilities; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CodeFixes; -using Microsoft.CodeAnalysis.Editing; -using Microsoft.CodeAnalysis.FindSymbols; -using Microsoft.CodeAnalysis.Operations; - -namespace Microsoft.CodeQuality.Analyzers.Maintainability -{ - /// - /// CA1801: Review unused parameters - /// - public abstract class ReviewUnusedParametersFixer : CodeFixProvider - { - public sealed override ImmutableArray FixableDiagnosticIds => ImmutableArray.Create(ReviewUnusedParametersAnalyzer.RuleId); - - public sealed override FixAllProvider GetFixAllProvider() - { - // See https://github.com/dotnet/roslyn/blob/master/docs/analyzers/FixAllProvider.md for more information on Fix All Providers - return WellKnownFixAllProviders.BatchFixer; - } - - public sealed override Task RegisterCodeFixesAsync(CodeFixContext context) - { - foreach (var diagnostic in context.Diagnostics) - { - context.RegisterCodeFix( - new MyCodeAction( - MicrosoftCodeQualityAnalyzersResources.RemoveUnusedParameterMessage, - async ct => await RemoveNodes(context.Document, diagnostic, ct).ConfigureAwait(false), - equivalenceKey: MicrosoftCodeQualityAnalyzersResources.RemoveUnusedParameterMessage), - diagnostic); - } - - return Task.CompletedTask; - } - - /// - /// Gets parameter declaration node for a given diagnostic node. - /// Requires language specific implementation because - /// diagnostics are reported on different syntax nodes for across languages. - /// - /// the diagnostic node - /// the parameter declaration node - protected abstract SyntaxNode GetParameterDeclarationNode(SyntaxNode node); - - /// - /// Checks if the node has a proper kind for a subnode for a object creation or an invocation node. - /// Requires language specific implementation because node kinds checked are language specific. - /// - protected abstract bool CanContinuouslyLeadToObjectCreationOrInvocation(SyntaxNode node); - - private async Task RemoveNodes(Document document, Diagnostic diagnostic, CancellationToken cancellationToken) - { - var solution = document.Project.Solution; - var pairs = await GetNodesToRemoveAsync(document, diagnostic, cancellationToken).ConfigureAwait(false); - foreach (var group in pairs.GroupBy(p => p.Key)) - { - DocumentEditor editor = await DocumentEditor.CreateAsync(solution.GetDocument(group.Key), cancellationToken).ConfigureAwait(false); - // Start removing from bottom to top to keep spans of nodes that are removed later. - foreach (var value in group.OrderByDescending(v => v.Value.SpanStart)) - { - editor.RemoveNode(value.Value); - } - - solution = solution.WithDocumentSyntaxRoot(group.Key, editor.GetChangedRoot()); - } - - return solution; - } - - private ImmutableArray? GetOperationArguments(SyntaxNode node, SemanticModel semanticModel, CancellationToken cancellationToken) - { - // For a given reference symbol node, find an object creation parent or an invocation parent. Then, return arguments of the parent found. - // For 'c(param1, param2)', the input node is 'c'. Then, we climb up to 'c(param1, param2)', and return [param1, param2]. - // For 'new A.B(param1, param2)', the input node is 'B'. Then, we climb up to 'new A.B(param1, param2)', and return [param1, param2]. - // For 'A.B.C(param1, param2)', the input node is 'C'. Then, we climb up to 'A.B.C(param1, param2)', and return [param1, param2]. - - // Consider operations like A.B.C(0). We start from 'C' and need to get to '0'. - // To achieve this, it is necessary to climb up to 'A.B.C' - // and check that this is IObjectCreationOperation or IInvocationOperation. - // Intermediate calls of GetOperation on 'B.C' and 'A.B.C.' return null. - // GetOperation on 'A.B.C(0)' returns a non-null operation. - // After that, it is possible to check its arguments. - // Return null in any unexpected situation, e.g. inconsistent tree. - while (node != null) - { - if (!CanContinuouslyLeadToObjectCreationOrInvocation(node)) - { - return null; - } - - node = node.Parent; - var operation = semanticModel.GetOperation(node, cancellationToken); - var arguments = (operation as IObjectCreationOperation)?.Arguments ?? (operation as IInvocationOperation)?.Arguments; - - if (arguments.HasValue) - { - return arguments.Value; - } - } - - return null; - } - - private async Task>> GetNodesToRemoveAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken) - { - SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); - SyntaxNode diagnosticNode = root.FindNode(diagnostic.Location.SourceSpan); - SyntaxNode declarationNode = GetParameterDeclarationNode(diagnosticNode); - - DocumentEditor editor = await DocumentEditor.CreateAsync(document, cancellationToken).ConfigureAwait(false); - ISymbol parameterSymbol = editor.SemanticModel.GetDeclaredSymbol(declarationNode, cancellationToken); - ISymbol methodDeclarationSymbol = parameterSymbol.ContainingSymbol; - - if (!IsSafeMethodToRemoveParameter(methodDeclarationSymbol)) - { - // See https://github.com/dotnet/roslyn-analyzers/issues/1466 - return ImmutableArray>.Empty; - } - - var nodesToRemove = ImmutableArray.CreateBuilder>(); - nodesToRemove.Add(new KeyValuePair(document.Id, declarationNode)); - var referencedSymbols = await SymbolFinder.FindReferencesAsync(methodDeclarationSymbol, document.Project.Solution, cancellationToken).ConfigureAwait(false); - - foreach (var referencedSymbol in referencedSymbols) - { - if (referencedSymbol.Locations != null) - { - foreach (var referenceLocation in referencedSymbol.Locations) - { - Location location = referenceLocation.Location; - var referenceRoot = location.SourceTree.GetRoot(cancellationToken); - var referencedSymbolNode = referenceRoot.FindNode(location.SourceSpan); - DocumentEditor localEditor = await DocumentEditor.CreateAsync(referenceLocation.Document, cancellationToken).ConfigureAwait(false); - var arguments = GetOperationArguments(referencedSymbolNode, localEditor.SemanticModel, cancellationToken); - - if (arguments != null) - { - foreach (IArgumentOperation argument in arguments) - { - // The name comparison below looks fragile. However, symbol comparison does not work for Reduced Extension Methods. Need to consider more reliable options. - if (string.Equals(argument.Parameter.Name, parameterSymbol.Name, StringComparison.Ordinal) && argument.ArgumentKind == ArgumentKind.Explicit) - { - nodesToRemove.Add(new KeyValuePair(referenceLocation.Document.Id, referenceRoot.FindNode(argument.Syntax.GetLocation().SourceSpan))); - } - } - } - } - } - } - - return nodesToRemove.ToImmutable(); - } - - private static bool IsSafeMethodToRemoveParameter(ISymbol methodDeclarationSymbol) - { - switch (methodDeclarationSymbol.Kind) - { - // Should not fix removing unused property indexer. - case SymbolKind.Property: - return false; - case SymbolKind.Method: - var methodSymbol = (IMethodSymbol)methodDeclarationSymbol; - // Should not remove parameter for a conversion operator. - return methodSymbol.MethodKind != MethodKind.Conversion; - default: - return true; - } - } - - private sealed class MyCodeAction : SolutionChangeAction - { - public MyCodeAction(string title, Func> createChangedSolution, string equivalenceKey) - : base(title, createChangedSolution, equivalenceKey) { } - } - } -} diff --git a/src/NetAnalyzers/Core/Microsoft.CodeQuality.Analyzers/Maintainability/ReviewUnusedParameters.cs b/src/NetAnalyzers/Core/Microsoft.CodeQuality.Analyzers/Maintainability/ReviewUnusedParameters.cs deleted file mode 100644 index 3174fdf251..0000000000 --- a/src/NetAnalyzers/Core/Microsoft.CodeQuality.Analyzers/Maintainability/ReviewUnusedParameters.cs +++ /dev/null @@ -1,252 +0,0 @@ -// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Collections.Concurrent; -using System.Collections.Immutable; -using System.Linq; -using Analyzer.Utilities; -using Analyzer.Utilities.Extensions; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.Diagnostics; -using Microsoft.CodeAnalysis.Operations; - -namespace Microsoft.CodeQuality.Analyzers.Maintainability -{ - /// - /// CA1801: Review unused parameters - /// - public abstract class ReviewUnusedParametersAnalyzer : DiagnosticAnalyzer - { - internal const string RuleId = "CA1801"; - - private static readonly LocalizableString s_localizableTitle = new LocalizableResourceString(nameof(MicrosoftCodeQualityAnalyzersResources.ReviewUnusedParametersTitle), MicrosoftCodeQualityAnalyzersResources.ResourceManager, typeof(MicrosoftCodeQualityAnalyzersResources)); - - private static readonly LocalizableString s_localizableMessage = new LocalizableResourceString(nameof(MicrosoftCodeQualityAnalyzersResources.ReviewUnusedParametersMessage), MicrosoftCodeQualityAnalyzersResources.ResourceManager, typeof(MicrosoftCodeQualityAnalyzersResources)); - private static readonly LocalizableString s_localizableDescription = new LocalizableResourceString(nameof(MicrosoftCodeQualityAnalyzersResources.ReviewUnusedParametersDescription), MicrosoftCodeQualityAnalyzersResources.ResourceManager, typeof(MicrosoftCodeQualityAnalyzersResources)); - - internal static DiagnosticDescriptor Rule = DiagnosticDescriptorHelper.Create(RuleId, - s_localizableTitle, - s_localizableMessage, - DiagnosticCategory.Usage, - RuleLevel.Disabled, // We have an implementation in IDE. - description: s_localizableDescription, - isPortedFxCopRule: true, - isDataflowRule: false); - - public sealed override ImmutableArray SupportedDiagnostics => ImmutableArray.Create(Rule); - - public sealed override void Initialize(AnalysisContext context) - { - context.EnableConcurrentExecution(); - - context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); - - context.RegisterCompilationStartAction(compilationStartContext => - { - var wellKnownTypeProvider = WellKnownTypeProvider.GetOrCreate(compilationStartContext.Compilation); - - INamedTypeSymbol? eventsArgSymbol = wellKnownTypeProvider.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemEventArgs); - - // Ignore conditional methods (FxCop compat - One conditional will often call another conditional method as its only use of a parameter) - INamedTypeSymbol? conditionalAttributeSymbol = wellKnownTypeProvider.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemDiagnosticsConditionalAttribute); - - // Ignore methods with special serialization attributes (FxCop compat - All serialization methods need to take 'StreamingContext') - INamedTypeSymbol? onDeserializingAttribute = wellKnownTypeProvider.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemRuntimeSerializationOnDeserializingAttribute); - INamedTypeSymbol? onDeserializedAttribute = wellKnownTypeProvider.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemRuntimeSerializationOnDeserializedAttribute); - INamedTypeSymbol? onSerializingAttribute = wellKnownTypeProvider.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemRuntimeSerializationOnSerializingAttribute); - INamedTypeSymbol? onSerializedAttribute = wellKnownTypeProvider.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemRuntimeSerializationOnSerializedAttribute); - INamedTypeSymbol? obsoleteAttribute = wellKnownTypeProvider.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemObsoleteAttribute); - - INamedTypeSymbol? serializationInfoType = wellKnownTypeProvider.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemRuntimeSerializationSerializationInfo); - INamedTypeSymbol? streamingContextType = wellKnownTypeProvider.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemRuntimeSerializationStreamingContext); - - ImmutableHashSet attributeSetForMethodsToIgnore = ImmutableHashSet.Create( - conditionalAttributeSymbol, - onDeserializedAttribute, - onDeserializingAttribute, - onSerializedAttribute, - onSerializingAttribute, - obsoleteAttribute); - - compilationStartContext.RegisterSymbolStartAction(symbolStartContext => - { - // Map from parameter to a bool indicating if the parameter is used or not. - var parameterUsageMap = new ConcurrentDictionary(); - - // Set of methods which are used as delegates. - var methodsUsedAsDelegates = new ConcurrentDictionary(); - - // Add candidate parameters for methods. - symbolStartContext.RegisterOperationBlockStartAction(startOperationBlockContext => - { - if (startOperationBlockContext.OwningSymbol is IMethodSymbol method && - ShouldAnalyzeMethod(method, startOperationBlockContext, eventsArgSymbol, attributeSetForMethodsToIgnore, serializationInfoType, streamingContextType)) - { - AddParameters(method, parameterUsageMap); - } - }); - - // Add candidate parameters for local functions. - symbolStartContext.RegisterOperationAction( - context => AddParameters(((ILocalFunctionOperation)context.Operation).Symbol, parameterUsageMap), - OperationKind.LocalFunction); - - // Add methods used as delegates. - symbolStartContext.RegisterOperationAction( - context => methodsUsedAsDelegates.TryAdd(((IMethodReferenceOperation)context.Operation).Method.OriginalDefinition, true), - OperationKind.MethodReference); - - // Mark parameters with a parameter reference as used. - symbolStartContext.RegisterOperationAction( - context => parameterUsageMap.AddOrUpdate( - ((IParameterReferenceOperation)context.Operation).Parameter, - addValue: true, - updateValueFactory: ReturnTrue), - OperationKind.ParameterReference); - - // Report unused parameters in SymbolEnd action. - symbolStartContext.RegisterSymbolEndAction( - context => ReportUnusedParameters(context, parameterUsageMap, methodsUsedAsDelegates)); - }, SymbolKind.NamedType); - }); - } - - private static bool ReturnTrue(IParameterSymbol param, bool value) => true; - - private static void AddParameters(IMethodSymbol method, ConcurrentDictionary unusedParameters) - { - foreach (var parameter in method.Parameters) - { - unusedParameters.TryAdd(parameter, false); - } - } - - private static void ReportUnusedParameters( - SymbolAnalysisContext symbolEndContext, - ConcurrentDictionary parameterUsageMap, - ConcurrentDictionary methodsUsedAsDelegates) - { - // Report diagnostics for unused parameters. - foreach (var (parameter, used) in parameterUsageMap) - { - if (used || parameter.Name.Length == 0) - { - continue; - } - - var containingMethod = (IMethodSymbol)parameter.ContainingSymbol; - - // Don't report parameters for methods used as delegates. - // We assume these methods have signature requirements, - // and hence its unused parameters cannot be removed. - if (methodsUsedAsDelegates.ContainsKey(containingMethod)) - { - continue; - } - - // Do not flag unused 'this' parameter of an extension method. - if (containingMethod.IsExtensionMethod && parameter.Ordinal == 0) - { - continue; - } - - // Do not flag unused parameters with special discard symbol name. - if (parameter.IsSymbolWithSpecialDiscardName()) - { - continue; - } - - var diagnostic = parameter.CreateDiagnostic(Rule, parameter.Name, parameter.ContainingSymbol.Name); - symbolEndContext.ReportDiagnostic(diagnostic); - } - } - -#pragma warning disable RS1012 // Start action has no registered actions. - private bool ShouldAnalyzeMethod( - IMethodSymbol method, - OperationBlockStartAnalysisContext startOperationBlockContext, - INamedTypeSymbol? eventsArgSymbol, - ImmutableHashSet attributeSetForMethodsToIgnore, - INamedTypeSymbol? serializationInfoType, - INamedTypeSymbol? streamingContextType) -#pragma warning restore RS1012 // Start action has no registered actions. - { - // We only care about methods with parameters. - if (method.Parameters.IsEmpty) - { - return false; - } - - // Ignore implicitly declared methods, extern methods, abstract methods, virtual methods, interface implementations and finalizers (FxCop compat). - if (method.IsImplicitlyDeclared || - method.IsExtern || - method.IsAbstract || - method.IsVirtual || - method.IsOverride || - method.IsImplementationOfAnyInterfaceMember() || - method.IsFinalizer()) - { - return false; - } - - // Ignore property accessors. - if (method.IsPropertyAccessor()) - { - return false; - } - - // Ignore primary constructor (body-less) of positional records. - if (IsPositionalRecordPrimaryConstructor(method)) - { - return false; - } - - // Ignore serialization special methods - if (method.IsSerializationConstructor(serializationInfoType, streamingContextType) || - method.IsGetObjectData(serializationInfoType, streamingContextType)) - { - return false; - } - - // Ignore event handler methods "Handler(object, MyEventArgs)" - if (method.HasEventHandlerSignature(eventsArgSymbol)) - { - return false; - } - - // Ignore methods with any attributes in 'attributeSetForMethodsToIgnore'. - if (method.GetAttributes().Any(a => a.AttributeClass != null && attributeSetForMethodsToIgnore.Contains(a.AttributeClass))) - { - return false; - } - - // Bail out if user has configured to skip analysis for the method. - if (!startOperationBlockContext.Options.MatchesConfiguredVisibility( - Rule, - method, - startOperationBlockContext.Compilation, - startOperationBlockContext.CancellationToken, - defaultRequiredVisibility: SymbolVisibilityGroup.All)) - { - return false; - } - - // Check to see if the method just throws a NotImplementedException/NotSupportedException - // We shouldn't warn about parameters in that case - if (startOperationBlockContext.IsMethodNotImplementedOrSupported()) - { - return false; - } - - // Ignore generated method for top level statements - if (method.IsTopLevelStatementsEntryPointMethod()) - { - return false; - } - - return true; - } - - protected abstract bool IsPositionalRecordPrimaryConstructor(IMethodSymbol methodSymbol); - } -} diff --git a/src/NetAnalyzers/Microsoft.CodeAnalysis.NetAnalyzers.md b/src/NetAnalyzers/Microsoft.CodeAnalysis.NetAnalyzers.md index 414dec219d..1450202fc9 100644 --- a/src/NetAnalyzers/Microsoft.CodeAnalysis.NetAnalyzers.md +++ b/src/NetAnalyzers/Microsoft.CodeAnalysis.NetAnalyzers.md @@ -948,18 +948,6 @@ Consistent naming of parameters in an override hierarchy increases the usability |CodeFix|True| --- -## [CA1801](https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1801): Review unused parameters - -Avoid unused paramereters in your code. If the parameter cannot be removed, then change its name so it starts with an underscore and is optionally followed by an integer, such as '_', '_1', '_2', etc. These are treated as special discard symbol names. - -|Item|Value| -|-|-| -|Category|Usage| -|Enabled|False| -|Severity|Warning| -|CodeFix|True| ---- - ## [CA1802](https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1802): Use literals where appropriate A field is declared static and read-only (Shared and ReadOnly in Visual Basic), and is initialized by using a value that is computable at compile time. Because the value that is assigned to the targeted field is computable at compile time, change the declaration to a const (Const in Visual Basic) field so that the value is computed at compile time instead of at run?time. diff --git a/src/NetAnalyzers/Microsoft.CodeAnalysis.NetAnalyzers.sarif b/src/NetAnalyzers/Microsoft.CodeAnalysis.NetAnalyzers.sarif index b7047e3d42..4b6d6f840a 100644 --- a/src/NetAnalyzers/Microsoft.CodeAnalysis.NetAnalyzers.sarif +++ b/src/NetAnalyzers/Microsoft.CodeAnalysis.NetAnalyzers.sarif @@ -147,26 +147,6 @@ ] } }, - "CA1801": { - "id": "CA1801", - "shortDescription": "Review unused parameters", - "fullDescription": "Avoid unused paramereters in your code. If the parameter cannot be removed, then change its name so it starts with an underscore and is optionally followed by an integer, such as '_', '_1', '_2', etc. These are treated as special discard symbol names.", - "defaultLevel": "warning", - "helpUri": "https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1801", - "properties": { - "category": "Usage", - "isEnabledByDefault": false, - "typeName": "CSharpReviewUnusedParametersAnalyzer", - "languages": [ - "C#" - ], - "tags": [ - "PortedFromFxCop", - "Telemetry", - "EnabledRuleInAggressiveMode" - ] - } - }, "CA1812": { "id": "CA1812", "shortDescription": "Avoid uninstantiated internal classes", @@ -5209,26 +5189,6 @@ ] } }, - "CA1801": { - "id": "CA1801", - "shortDescription": "Review unused parameters", - "fullDescription": "Avoid unused paramereters in your code. If the parameter cannot be removed, then change its name so it starts with an underscore and is optionally followed by an integer, such as '_', '_1', '_2', etc. These are treated as special discard symbol names.", - "defaultLevel": "warning", - "helpUri": "https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1801", - "properties": { - "category": "Usage", - "isEnabledByDefault": false, - "typeName": "BasicReviewUnusedParametersAnalyzer", - "languages": [ - "Visual Basic" - ], - "tags": [ - "PortedFromFxCop", - "Telemetry", - "EnabledRuleInAggressiveMode" - ] - } - }, "CA1812": { "id": "CA1812", "shortDescription": "Avoid uninstantiated internal classes", diff --git a/src/NetAnalyzers/UnitTests/Microsoft.CodeQuality.Analyzers/Maintainability/ReviewUnusedParametersTests.Fixer.cs b/src/NetAnalyzers/UnitTests/Microsoft.CodeQuality.Analyzers/Maintainability/ReviewUnusedParametersTests.Fixer.cs deleted file mode 100644 index f1b2c1bb6e..0000000000 --- a/src/NetAnalyzers/UnitTests/Microsoft.CodeQuality.Analyzers/Maintainability/ReviewUnusedParametersTests.Fixer.cs +++ /dev/null @@ -1,896 +0,0 @@ -// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System.Threading.Tasks; -using Microsoft.CodeAnalysis.Testing; -using Xunit; -using VerifyCS = Test.Utilities.CSharpCodeFixVerifier< - Microsoft.CodeQuality.CSharp.Analyzers.Maintainability.CSharpReviewUnusedParametersAnalyzer, - Microsoft.CodeQuality.CSharp.Analyzers.Maintainability.CSharpReviewUnusedParametersFixer>; -using VerifyVB = Test.Utilities.VisualBasicCodeFixVerifier< - Microsoft.CodeQuality.VisualBasic.Analyzers.Maintainability.BasicReviewUnusedParametersAnalyzer, - Microsoft.CodeQuality.VisualBasic.Analyzers.Maintainability.BasicReviewUnusedParametersFixer>; - -namespace Microsoft.CodeQuality.Analyzers.Maintainability.UnitTests -{ - public class ReviewUnusedParametersFixerTests - { - [Fact] - public async Task BaseScenario_CSharp() - { - var code = @" -using System; - -class C -{ - public int Property1 { get; set; } - - public int Field1; - - public C(int [|param|]) - { - } - - public void UnusedParamMethod(int [|param|]) - { - } - - public static void UnusedParamStaticMethod(int [|param1|]) - { - } - - public void UnusedDefaultParamMethod(int [|defaultParam|] = 1) - { - } - - public void UnusedParamsArrayParamMethod(params int[] [|paramsArr|]) - { - } - - public void MultipleUnusedParamsMethod(int [|param1|], int [|param2|]) - { - } - - private void UnusedRefParamMethod(ref int [|param1|]) - { - } - - public void UnusedErrorTypeParamMethod({|CS0246:UndefinedType|} [|param1|]) // error CS0246: The type or namespace name 'UndefinedType' could not be found. - { - } - - public void Caller() - { - var c = new C(0); - UnusedParamMethod(this.Property1); - int b = 0; - UnusedParamMethod(b); - UnusedParamStaticMethod(1 + 1); - UnusedDefaultParamMethod(this.Field1); - UnusedParamsArrayParamMethod(new int[0]); - MultipleUnusedParamsMethod(0, 1); - int a = 0; - UnusedRefParamMethod(ref a); - } -} -"; - var fix = @" -using System; - -class C -{ - public int Property1 { get; set; } - - public int Field1; - - public C() - { - } - - public void UnusedParamMethod() - { - } - - public static void UnusedParamStaticMethod() - { - } - - public void UnusedDefaultParamMethod() - { - } - - public void UnusedParamsArrayParamMethod() - { - } - - public void MultipleUnusedParamsMethod() - { - } - - private void UnusedRefParamMethod() - { - } - - public void UnusedErrorTypeParamMethod() // error CS0246: The type or namespace name 'UndefinedType' could not be found. - { - } - - public void Caller() - { - var c = new C(); - UnusedParamMethod(); - int b = 0; - UnusedParamMethod(); - UnusedParamStaticMethod(); - UnusedDefaultParamMethod(); - UnusedParamsArrayParamMethod(); - MultipleUnusedParamsMethod(); - int a = 0; - UnusedRefParamMethod(); - } -} -"; - await new VerifyCS.Test - { - TestState = { Sources = { code } }, - FixedState = { Sources = { fix } }, - NumberOfFixAllIterations = 2, - }.RunAsync(); - } - - [Fact] - public async Task ExternalFileScenario_CSharp() - { - var code = @" -class C -{ - public static void UnusedParamStaticMethod(int [|param1|]) - { - } -} - -class D -{ - public void Caller() - { - C.UnusedParamStaticMethod(0); - E.M(0); - } -} -"; - var fix = @" -class C -{ - public static void UnusedParamStaticMethod() - { - } -} - -class D -{ - public void Caller() - { - C.UnusedParamStaticMethod(); - E.M(); - } -} -"; - - var anotherCode = @" -class E -{ - public static void M(int [|param1|]) { } -} -"; - var anotherCodeFix = @" -class E -{ - public static void M() { } -} -"; - await new VerifyCS.Test - { - TestState = - { - Sources = - { - code, - anotherCode, - }, - }, - FixedState = - { - Sources = - { - fix, - anotherCodeFix, - }, - }, - }.RunAsync(); - } - - [Fact] - public async Task CommentsNearParams_CSharp() - { - var code = @" -class C -{ - public C(/* comment left */ int /* comment middle */ [|param|] /* comment right */) - { - } - - public int M(/* comment 1 */ int /* comment 2 */ [|param1|] /* comment 3 */, /* comment 4 */ int /* comment 5 */ param2 /* comment 6 */) - { - return param2; - } - - public void Caller() - { - var c = new C(/* caller comment left */ 0 /* caller comment right */); - M(/* comment 1 */ 0 /* comment 2 */, /* comment 3 */ 1 /* comment 4 */); - } -} -"; - var fix = @" -class C -{ - public C(/* comment left */ ) - { - } - - public int M(/* comment 1 */ int /* comment 5 */ param2 /* comment 6 */) - { - return param2; - } - - public void Caller() - { - var c = new C(/* caller comment left */ ); - M(/* comment 1 */ 1 /* comment 4 */); - } -} -"; - await VerifyCS.VerifyCodeFixAsync(code, fix); - } - - [Fact] - public async Task NamedParams_CSharp() - { - var code = @" -class C -{ - public int UnusedParamMethod(int param1, int [|param2|]) - { - return param1; - } - - public void Caller() - { - UnusedParamMethod(param2: 0, param1: 1); - } -} -"; - var fix = @" -class C -{ - public int UnusedParamMethod(int param1) - { - return param1; - } - - public void Caller() - { - UnusedParamMethod(param1: 1); - } -} -"; - await VerifyCS.VerifyCodeFixAsync(code, fix); - } - - [Fact] - public async Task MultipleNamespaces_CSharp() - { - var code = @" -namespace A.B.C.D -{ - public class Test - { - public Test(int [|param1|]) { } - - public static void UnusedParamMethod(int [|param1|]) { } - } -} - -namespace E -{ - class CallerClass - { - public void Caller() - { - var test = new A.B.C.D.Test(0); - A.B.C.D.Test.UnusedParamMethod(0); - } - } -} -"; - var fix = @" -namespace A.B.C.D -{ - public class Test - { - public Test() { } - - public static void UnusedParamMethod() { } - } -} - -namespace E -{ - class CallerClass - { - public void Caller() - { - var test = new A.B.C.D.Test(); - A.B.C.D.Test.UnusedParamMethod(); - } - } -} -"; - await VerifyCS.VerifyCodeFixAsync(code, fix); - } - - [Fact] - public async Task CalculationsInParameter_CSharp() - { - var code = @" -class C -{ - void M() { } - - int N(int x) => x; - - void Caller() - { - M(); - } -} -"; - await VerifyCS.VerifyCodeFixAsync(code, code); - } - - [Fact] - public async Task Conversion_CSharp() - { - var code = @" -class C -{ - public static explicit operator int(C [|value|]) => 0; - - public void M1(double [|d|]) { } - - public void M2(int [|i|]) { } - - public void M3(int [|x|]) { } - - public void Caller() - { - int i = 0; - M1(i); - double d = 0; - M2((int)d); - var instance = new C(); - M3((int)instance); - } -} -"; - var fix = @" -class C -{ - public static explicit operator int(C [|value|]) => 0; - - public void M1() { } - - public void M2() { } - - public void M3() { } - - public void Caller() - { - int i = 0; - M1(); - double d = 0; - M2(); - var instance = new C(); - M3(); - } -} -"; - await new VerifyCS.Test - { - TestState = - { - Sources = { code }, - }, - FixedState = - { - Sources = { fix }, - MarkupHandling = MarkupMode.Allow, - }, - }.RunAsync(); - } - - [Fact] - public async Task ExtensionMethod_CSharp() - { - var code = @" -static class C -{ - static void ExtensionMethod(this int i) { } - static void ExtensionMethod(this int i, int [|anotherParam|]) { } - - static void Caller() - { - int i = 0; - i.ExtensionMethod(); - i.ExtensionMethod(i); - } -} -"; - var fix = @" -static class C -{ - static void ExtensionMethod(this int i) { } - static void {|CS0111:ExtensionMethod|}(this int i) { } - - static void Caller() - { - int i = 0; - i.{|CS0121:ExtensionMethod|}(); - i.{|CS0121:ExtensionMethod|}(); - } -} -"; - await VerifyCS.VerifyCodeFixAsync(code, fix); - } - - [Fact(Skip = "https://github.com/dotnet/roslyn/issues/22449")] - public async Task DictionaryConstructor_CSharp() - { - var code = @" -using System.Collections.Generic; - -class Dict : Dictionary -{ - public void Add(int key, int a, int [|b|]) - { - var val = new MyValue(); - val.A = a; - this.Add(key, val); - } - - public static Dict Create() - { - return new Dict() - { - {0, 1, 2} - }; - } -} - -class MyValue -{ - public int A; -} -"; - - var fix = @" -using System.Collections.Generic; - -class Dict : Dictionary -{ - public void Add(int key, int a) - { - var val = new MyValue(); - val.A = a; - this.Add(key, val); - } - - public static Dict Create() - { - return new Dict() - { - {0, 1} - }; - } -} - -class MyValue -{ - public int A; -} -"; - await VerifyCS.VerifyCodeFixAsync(code, fix); - } - - [Fact] - public async Task BaseScenario_Basic() - { - var code = @" -Class C - Public Property Property1 As Integer - - Public Field1 As Integer - - Public Sub New([|param|] As Integer) - End Sub - - Public Sub UnusedParamMethod([|param|] As Integer) - End Sub - - Public Shared Sub UnusedParamStaticMethod([|param1|] As Integer) - End Sub - - Public Sub UnusedDefaultParamMethod(Optional [|defaultParam|] As Integer = 1) - End Sub - - Public Sub UnusedParamsArrayParamMethod(ParamArray [|paramsArr|] As Integer()) - End Sub - - Public Sub MultipleUnusedParamsMethod([|param1|] As Integer, [|param2|] As Integer) - End Sub - - Private Sub UnusedRefParamMethod(ByRef [|param1|] As Integer) - End Sub - - Public Sub UnusedErrorTypeParamMethod([|param1|] As {|BC30002:UndefinedType|}) ' error BC30002: Type 'UndefinedType' is not defined. - End Sub - - Public Sub Caller() - Dim c = New C(0) - UnusedParamMethod(Property1) - Dim b As Integer = 0 - UnusedParamMethod(b) - UnusedParamStaticMethod(1 + 1) - UnusedDefaultParamMethod(Field1) - UnusedParamsArrayParamMethod(New Integer() {}) - MultipleUnusedParamsMethod(0, 1) - Dim a As Integer = 0 - UnusedRefParamMethod(a) - End Sub -End Class -"; - var fix = @" -Class C - Public Property Property1 As Integer - - Public Field1 As Integer - - Public Sub New() - End Sub - - Public Sub UnusedParamMethod() - End Sub - - Public Shared Sub UnusedParamStaticMethod() - End Sub - - Public Sub UnusedDefaultParamMethod() - End Sub - - Public Sub UnusedParamsArrayParamMethod() - End Sub - - Public Sub MultipleUnusedParamsMethod() - End Sub - - Private Sub UnusedRefParamMethod() - End Sub - - Public Sub UnusedErrorTypeParamMethod() ' error BC30002: Type 'UndefinedType' is not defined. - End Sub - - Public Sub Caller() - Dim c = New C() - UnusedParamMethod() - Dim b As Integer = 0 - UnusedParamMethod() - UnusedParamStaticMethod() - UnusedDefaultParamMethod() - UnusedParamsArrayParamMethod() - MultipleUnusedParamsMethod() - Dim a As Integer = 0 - UnusedRefParamMethod() - End Sub -End Class -"; - await new VerifyVB.Test - { - TestState = { Sources = { code } }, - FixedState = { Sources = { fix } }, - NumberOfFixAllIterations = 2, - }.RunAsync(); - } - - [Fact] - public async Task ExternalFileScenario_Basic() - { - var code = @" -Class C - Public Shared Sub UnusedParamStaticMethod([|param1|] As Integer) - End Sub -End Class - -Class D - Public Sub Caller() - C.UnusedParamStaticMethod(0) - E.M(0) - End Sub -End Class -"; - var fix = @" -Class C - Public Shared Sub UnusedParamStaticMethod() - End Sub -End Class - -Class D - Public Sub Caller() - C.UnusedParamStaticMethod() - E.M() - End Sub -End Class -"; - - var anotherCode = @" -Class E - Public Shared Sub M([|param1|] As Integer) - End Sub -End Class -"; - var anotherCodeFix = @" -Class E - Public Shared Sub M() - End Sub -End Class -"; - await new VerifyVB.Test - { - TestState = - { - Sources = - { - code, - anotherCode, - }, - }, - FixedState = - { - Sources = - { - fix, - anotherCodeFix, - }, - }, - }.RunAsync(); - } - - [Fact] - public async Task NamedParams_Basic() - { - var code = @" -Class C - Public Function UnusedParamMethod(param1 As Integer, [|param2|] As Integer) As Integer - Return param1 - End Function - - Public Sub Caller() - UnusedParamMethod(param2:=0, param1:=1) - End Sub -End Class -"; - var fix = @" -Class C - Public Function UnusedParamMethod(param1 As Integer) As Integer - Return param1 - End Function - - Public Sub Caller() - UnusedParamMethod(param1:=1) - End Sub -End Class -"; - await VerifyVB.VerifyCodeFixAsync(code, fix); - } - - [Fact] - public async Task MultipleNamespaces_Basic() - { - var code = @" -Namespace A.B.C.D - Public Class Test - Public Sub New([|param1|] As Integer) - End Sub - - Public Shared Sub UnusedParamMethod([|param1|] As Integer) - End Sub - End Class -End Namespace - -Namespace E - Class CallerClass - Public Sub Caller() - Dim test = New A.B.C.D.Test(0) - A.B.C.D.Test.UnusedParamMethod(0) - End Sub - End Class -End Namespace -"; - var fix = @" -Namespace A.B.C.D - Public Class Test - Public Sub New() - End Sub - - Public Shared Sub UnusedParamMethod() - End Sub - End Class -End Namespace - -Namespace E - Class CallerClass - Public Sub Caller() - Dim test = New A.B.C.D.Test() - A.B.C.D.Test.UnusedParamMethod() - End Sub - End Class -End Namespace -"; - await VerifyVB.VerifyCodeFixAsync(code, fix); - } - - [Fact] - public async Task CalculationsInParameter_Basic() - { - var code = @" -Class C - Sub M([|x|] As Integer) - End Sub - - Function N(x As Integer) As Integer - Return x - End Function - - Sub Caller() - M(N(0)) - End Sub -End Class -"; - - var fix = @" -Class C - Sub M() - End Sub - - Function N(x As Integer) As Integer - Return x - End Function - - Sub Caller() - M() - End Sub -End Class -"; - await VerifyVB.VerifyCodeFixAsync(code, fix); - } - - [Fact] - public async Task Conversion_Basic() - { - var code = @" -Class C - Public Shared Narrowing Operator CType([|value|] As C) As Integer - Return 0 - End Operator - - Public Sub M1([|d|] As Double) - End Sub - - Public Sub M2([|i|] As Integer) - End Sub - - Public Sub M3([|x|] As Integer) - End Sub - - Public Sub Caller() - Dim i As Integer = 0 - M1(i) - Dim d As Double = 0 - M2(CInt(d)) - Dim instance = New C() - M3(CType(instance, Integer)) - End Sub -End Class -"; - var fix = @" -Class C - Public Shared Narrowing Operator CType([|value|] As C) As Integer - Return 0 - End Operator - - Public Sub M1() - End Sub - - Public Sub M2() - End Sub - - Public Sub M3() - End Sub - - Public Sub Caller() - Dim i As Integer = 0 - M1() - Dim d As Double = 0 - M2() - Dim instance = New C() - M3() - End Sub -End Class -"; - await new VerifyVB.Test - { - TestState = - { - Sources = { code }, - }, - FixedState = - { - Sources = { fix }, - MarkupHandling = MarkupMode.Allow, - }, - }.RunAsync(); - } - - [Fact] - public async Task ExtensionMethod_Basic() - { - var code = @" -Imports System.Runtime.CompilerServices - -Module D - - Public Sub ExtensionMethod(s As String) - End Sub - - - Public Sub ExtensionMethod(s As String, [|i|] As Integer) - End Sub - - Sub Caller() - Dim s as String - s.ExtensionMethod() - s.ExtensionMethod(0) - End Sub -End Module -"; - var fix = @" -Imports System.Runtime.CompilerServices - -Module D - - Public Sub {|BC30269:ExtensionMethod|}(s As String) - End Sub - - - Public Sub ExtensionMethod(s As String) - End Sub - - Sub Caller() - Dim s as String - s.{|BC30521:ExtensionMethod|}() - s.{|BC30521:ExtensionMethod|}() - End Sub -End Module -"; - await VerifyVB.VerifyCodeFixAsync(code, fix); - } - } -} \ No newline at end of file diff --git a/src/NetAnalyzers/UnitTests/Microsoft.CodeQuality.Analyzers/Maintainability/ReviewUnusedParametersTests.cs b/src/NetAnalyzers/UnitTests/Microsoft.CodeQuality.Analyzers/Maintainability/ReviewUnusedParametersTests.cs deleted file mode 100644 index 10ebc67610..0000000000 --- a/src/NetAnalyzers/UnitTests/Microsoft.CodeQuality.Analyzers/Maintainability/ReviewUnusedParametersTests.cs +++ /dev/null @@ -1,1422 +0,0 @@ -// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.Testing; -using Microsoft.CodeAnalysis.Text; -using Test.Utilities; -using Xunit; -using VerifyCS = Test.Utilities.CSharpCodeFixVerifier< - Microsoft.CodeQuality.CSharp.Analyzers.Maintainability.CSharpReviewUnusedParametersAnalyzer, - Microsoft.CodeQuality.CSharp.Analyzers.Maintainability.CSharpReviewUnusedParametersFixer>; -using VerifyVB = Test.Utilities.VisualBasicCodeFixVerifier< - Microsoft.CodeQuality.VisualBasic.Analyzers.Maintainability.BasicReviewUnusedParametersAnalyzer, - Microsoft.CodeQuality.VisualBasic.Analyzers.Maintainability.BasicReviewUnusedParametersFixer>; - -namespace Microsoft.CodeQuality.Analyzers.Maintainability.UnitTests -{ - public class ReviewUnusedParametersTests - { - #region Unit tests for no analyzer diagnostic - [Fact] - [WorkItem(4039, "https://github.com/dotnet/roslyn-analyzers/issues/4039")] - public async Task NoDiagnosticForUnnamedParameterTest() - { - await VerifyCS.VerifyAnalyzerAsync(@" -public class NeatCode -{ - public void DoSomething(string) - { - } -} -", DiagnosticResult.CompilerError("CS1001").WithLocation(4, 35)); - } - - [Fact] - [WorkItem(459, "https://github.com/dotnet/roslyn-analyzers/issues/459")] - public async Task NoDiagnosticSimpleCasesTest() - { - await VerifyCS.VerifyAnalyzerAsync(@" -using System; - -public class NeatCode -{ - // Used parameter methods - public void UsedParameterMethod1(string use) - { - Console.WriteLine(this); - Console.WriteLine(use); - } - - public void UsedParameterMethod2(string use) - { - UsedParameterMethod3(ref use); - } - - public void UsedParameterMethod3(ref string use) - { - use = null; - } -} -"); - - await VerifyVB.VerifyAnalyzerAsync(@" -Imports System - -Public Class NeatCode - ' Used parameter methods - Public Sub UsedParameterMethod1(use As String) - Console.WriteLine(Me) - Console.WriteLine(use) - End Sub - - Public Sub UsedParameterMethod2(use As String) - UsedParameterMethod3(use) - End Sub - - Public Sub UsedParameterMethod3(ByRef use As String) - use = Nothing - End Sub -End Class -"); - } - - [Fact] - [WorkItem(459, "https://github.com/dotnet/roslyn-analyzers/issues/459")] - public async Task NoDiagnosticDelegateTest() - { - await VerifyCS.VerifyAnalyzerAsync(@" -using System; - -public class NeatCode -{ - // Used parameter methods - public void UsedParameterMethod1(Action a) - { - a(); - } - - public void UsedParameterMethod2(Action a1, Action a2) - { - try - { - a1(); - } - catch(Exception) - { - a2(); - } - } -} -"); - - await VerifyVB.VerifyAnalyzerAsync(@" -Imports System - -Public Class NeatCode - ' Used parameter methods - Public Sub UsedParameterMethod1(a As Action) - a() - End Sub - - Public Sub UsedParameterMethod2(a1 As Action, a2 As Action) - Try - a1() - Catch generatedExceptionName As Exception - a2() - End Try - End Sub -End Class -"); - } - - [Fact] - [WorkItem(8884, "https://github.com/dotnet/roslyn/issues/8884")] - public async Task NoDiagnosticDelegateTest2_CSharp() - { - await VerifyCS.VerifyAnalyzerAsync(@" -using System; - -public class NeatCode -{ - // Used parameter methods - public void UsedParameterMethod1(Action a) - { - Action a2 = new Action(() => - { - a(); - }); - } -}"); - } - - [Fact] - [WorkItem(459, "https://github.com/dotnet/roslyn-analyzers/issues/459")] - public async Task NoDiagnosticDelegateTest2_VB() - { - await VerifyVB.VerifyAnalyzerAsync(@" -Imports System - -Public Class NeatCode - ' Used parameter methods - Public Sub UsedParameterMethod1(a As Action) - Dim a2 As New Action(Sub() - a() - End Sub) - End Sub -End Class -"); - } - - [Fact] - [WorkItem(8884, "https://github.com/dotnet/roslyn/issues/8884")] - public async Task NoDiagnosticUsingTest_CSharp() - { - await VerifyCS.VerifyAnalyzerAsync(@" -using System; - -class C -{ - void F(int x, IDisposable o) - { - using (o) - { - int y = x; - } - } -} -"); - } - - [Fact] - [WorkItem(8884, "https://github.com/dotnet/roslyn/issues/8884")] - public async Task NoDiagnosticUsingTest_VB() - { - await VerifyVB.VerifyAnalyzerAsync(@" -Imports System - -Class C - Private Sub F(x As Integer, o As IDisposable) - Using o - Dim y As Integer = x - End Using - End Sub -End Class -"); - } - - [Fact] - [WorkItem(8884, "https://github.com/dotnet/roslyn/issues/8884")] - public async Task NoDiagnosticLinqTest_CSharp() - { - await VerifyCS.VerifyAnalyzerAsync(@" -using System; -using System.Linq; -using System.Reflection; - -class C -{ - private object F(Assembly assembly) - { - var type = (from t in assembly.GetTypes() - select t.Attributes).FirstOrDefault(); - return type; - } -} -"); - } - - [Fact] - [WorkItem(8884, "https://github.com/dotnet/roslyn/issues/8884")] - public async Task NoDiagnosticLinqTest_VB() - { - await VerifyVB.VerifyAnalyzerAsync(@" -Imports System -Imports System.Linq -Imports System.Reflection - -Class C - Private Function F(assembly As Assembly) As Object - Dim type = (From t In assembly.DefinedTypes() Select t.Attributes).FirstOrDefault() - Return type - End Function -End Class -"); - } - - [Fact] - [WorkItem(459, "https://github.com/dotnet/roslyn-analyzers/issues/459")] - public async Task NoDiagnosticSpecialCasesTest() - { - await VerifyCS.VerifyAnalyzerAsync(@" -using System; -using System.Runtime.InteropServices; - -public abstract class Derived : Base, I -{ - // Override - public override void VirtualMethod(int param) - { - } - - // Abstract - public abstract void AbstractMethod(int param); - - // Implicit interface implementation - public void Method1(int param) - { - } - - // Explicit interface implementation - void I.Method2(int param) - { - } - - // Event handlers - public void MyEventHandler(object o, EventArgs e) - { - } - - public void MyEventHandler2(object o, MyEventArgs e) - { - } - - public class MyEventArgs : EventArgs { } -} - -public class Base -{ - // Virtual - public virtual void VirtualMethod(int param) - { - } -} - -public interface I -{ - void Method1(int param); - void Method2(int param); -} - -public class ClassWithExtern -{ - [DllImport(""Dependency.dll"")] - public static extern void DllImportMethod(int param); - - public static extern void ExternalMethod(int param); -} -"); - - await VerifyVB.VerifyAnalyzerAsync(@" -Imports System -Imports System.Runtime.InteropServices - -Public MustInherit Class Derived - Inherits Base - Implements I - ' Override - Public Overrides Sub VirtualMethod(param As Integer) - End Sub - - ' Abstract - Public MustOverride Sub AbstractMethod(param As Integer) - - ' Explicit interface implementation - VB has no implicit interface implementation. - Public Sub Method1(param As Integer) Implements I.Method1 - End Sub - - ' Explicit interface implementation - Private Sub I_Method2(param As Integer) Implements I.Method2 - End Sub - - ' Event handlers - Public Sub MyEventHandler(o As Object, e As EventArgs) - End Sub - - Public Sub MyEventHandler2(o As Object, e As MyEventArgs) - End Sub - - Public Class MyEventArgs - Inherits EventArgs - End Class -End Class - -Public Class Base - ' Virtual - Public Overridable Sub VirtualMethod(param As Integer) - End Sub -End Class - -Public Interface I - Sub Method1(param As Integer) - Sub Method2(param As Integer) -End Interface - -Public Class ClassWithExtern - - Public Shared Sub DllImportMethod(param As Integer) - End Sub - - Public Declare Function DeclareFunction Lib ""Dependency.dll"" (param As Integer) As Integer -End Class -"); - } - - [Fact] - [WorkItem(459, "https://github.com/dotnet/roslyn-analyzers/issues/459")] - public async Task NoDiagnosticForMethodsWithSpecialAttributesTest() - { - await VerifyCS.VerifyAnalyzerAsync(@" -#define CONDITION_1 - -using System; -using System.Diagnostics; -using System.Runtime.Serialization; - -public class ConditionalMethodsClass -{ - [Conditional(""CONDITION_1"")] - private static void ConditionalMethod(int a) - { - AnotherConditionalMethod(a); - } - - [Conditional(""CONDITION_2"")] - private static void AnotherConditionalMethod(int b) - { - Console.WriteLine(b); - } -} - -public class SerializableMethodsClass -{ - [OnSerializing] - private void OnSerializingCallback(StreamingContext context) - { - Console.WriteLine(this); - } - - [OnSerialized] - private void OnSerializedCallback(StreamingContext context) - { - Console.WriteLine(this); - } - - [OnDeserializing] - private void OnDeserializingCallback(StreamingContext context) - { - Console.WriteLine(this); - } - - [OnDeserialized] - private void OnDeserializedCallback(StreamingContext context) - { - Console.WriteLine(this); - } -} -"); - - await VerifyVB.VerifyAnalyzerAsync(@" -#Const CONDITION_1 = 5 - -Imports System -Imports System.Diagnostics -Imports System.Runtime.Serialization - -Public Class ConditionalMethodsClass - _ - Private Shared Sub ConditionalMethod(a As Integer) - AnotherConditionalMethod(a) - End Sub - - _ - Private Shared Sub AnotherConditionalMethod(b As Integer) - Console.WriteLine(b) - End Sub -End Class - -Public Class SerializableMethodsClass - _ - Private Sub OnSerializingCallback(context As StreamingContext) - Console.WriteLine(Me) - End Sub - - _ - Private Sub OnSerializedCallback(context As StreamingContext) - Console.WriteLine(Me) - End Sub - - _ - Private Sub OnDeserializingCallback(context As StreamingContext) - Console.WriteLine(Me) - End Sub - - _ - Private Sub OnDeserializedCallback(context As StreamingContext) - Console.WriteLine(Me) - End Sub -End Class -"); - } - - [Fact, WorkItem(1218, "https://github.com/dotnet/roslyn-analyzers/issues/1218")] - public async Task NoDiagnosticForMethodsUsedAsDelegatesCSharp() - { - await VerifyCS.VerifyAnalyzerAsync(@" -using System; - -public class C1 -{ - private Action _handler; - - public void Handler(object o1) - { - } - - public void SetupHandler() - { - _handler = Handler; - } -} - -public class C2 -{ - public void Handler(object o1) - { - } - - public void TakesHandler(Action handler) - { - handler(null); - } - - public void SetupHandler() - { - TakesHandler(Handler); - } -} - -public class C3 -{ - private Action _handler; - - public C3() - { - _handler = Handler; - } - - public void Handler(object o1) - { - } -}"); - } - - [Fact, WorkItem(1218, "https://github.com/dotnet/roslyn-analyzers/issues/1218")] - public async Task NoDiagnosticForMethodsUsedAsDelegatesBasic() - { - await VerifyVB.VerifyAnalyzerAsync(@" -Imports System -Public Class C1 - Private _handler As Action(Of Object) - - Public Sub Handler(o As Object) - End Sub - - Public Sub SetupHandler() - _handler = AddressOf Handler - End Sub -End Class - -Module M2 - Sub Handler(o As Object) - End Sub - - Sub TakesHandler(handler As Action(Of Object)) - handler(Nothing) - End Sub - - Sub SetupHandler() - TakesHandler(AddressOf Handler) - End Sub -End Module - -Class C3 - Private _handler As Action(Of Object) - - Sub New() - _handler = AddressOf Handler - End Sub - - Sub Handler(o As Object) - End Sub -End Class -"); - } - - [Fact, WorkItem(1218, "https://github.com/dotnet/roslyn-analyzers/issues/1218")] - public async Task NoDiagnosticForObsoleteMethods() - { - await VerifyCS.VerifyAnalyzerAsync(@" -using System; - -public class C1 -{ - [Obsolete] - public void ObsoleteMethod(object o1) - { - } -}"); - - await VerifyVB.VerifyAnalyzerAsync(@" -Imports System - -Public Class C1 - - Public Sub ObsoleteMethod(o1 as Object) - End Sub -End Class"); - } - - [Fact, WorkItem(1218, "https://github.com/dotnet/roslyn-analyzers/issues/1218")] - public async Task NoDiagnosticMethodJustThrowsNotImplemented() - { - await VerifyCS.VerifyAnalyzerAsync(@" -using System; - -public class MyAttribute: Attribute -{ - public int X; - - public MyAttribute(int x) - { - X = x; - } -} -public class C1 -{ - public int Prop1 - { - get - { - throw new NotImplementedException(); - } - set - { - throw new NotImplementedException(); - } - } - - public void Method1(object o1) - { - throw new NotImplementedException(); - } - - public void Method2(object o1) => throw new NotImplementedException(); - - [MyAttribute(0)] - public void Method3(object o1) - { - throw new NotImplementedException(); - } -}"); - - await VerifyVB.VerifyAnalyzerAsync(@" -Imports System - -Public Class C1 - Property Prop1 As Integer - Get - Throw New NotImplementedException() - End Get - Set(ByVal value As Integer) - Throw New NotImplementedException() - End Set - End Property - - Public Sub Method1(o1 As Object) - Throw New NotImplementedException() - End Sub -End Class"); - } - - [Fact, WorkItem(1218, "https://github.com/dotnet/roslyn-analyzers/issues/1218")] - public async Task NoDiagnosticMethodJustThrowsNotSupported() - { - await VerifyCS.VerifyAnalyzerAsync(@" -using System; - -public class C1 -{ - public int Prop1 - { - get - { - throw new NotSupportedException(); - } - set - { - throw new NotSupportedException(); - } - } - - public void Method1(object o1) - { - throw new NotSupportedException(); - } - - public void Method2(object o1) => throw new NotSupportedException(); -}"); - - await VerifyVB.VerifyAnalyzerAsync(@" -Imports System - -Public Class C1 - Property Prop1 As Integer - Get - Throw New NotSupportedException() - End Get - Set(ByVal value As Integer) - Throw New NotSupportedException() - End Set - End Property - - Public Sub Method1(o1 As Object) - Throw New NotSupportedException() - End Sub -End Class"); - } - - [Fact] - public async Task NoDiagnosticsForIndexer() - { - await VerifyCS.VerifyAnalyzerAsync(@" -class C -{ - public int this[int i] - { - get { return 0; } - set { } - } -} -"); - - await VerifyVB.VerifyAnalyzerAsync(@" -Class C - Public Property Item(i As Integer) As Integer - Get - Return 0 - End Get - - Set - End Set - End Property -End Class -"); - } - - [Fact] - public async Task NoDiagnosticsForPropertySetter() - { - await VerifyCS.VerifyAnalyzerAsync(@" -class C -{ - public int Property - { - get { return 0; } - set { } - } -} -"); - - await VerifyVB.VerifyAnalyzerAsync(@" -Class C - Public Property Property1 As Integer - Get - Return 0 - End Get - - Set - End Set - End Property -End Class -"); - } - [Fact] - public async Task NoDiagnosticsForFirstParameterOfExtensionMethod() - { - await VerifyCS.VerifyAnalyzerAsync(@" -static class C -{ - static void ExtensionMethod(this int i) { } - static int ExtensionMethod(this int i, int anotherParam) { return anotherParam; } -} -"); - } - - [Fact] - public async Task NoDiagnosticsForSingleStatementMethodsWithDefaultParameters() - { - await VerifyCS.VerifyAnalyzerAsync(@" -using System; - -public class C -{ - public void SomeMethod(string p1, string p2 = null) - { - throw new NotImplementedException(); - } -} -"); - - await VerifyVB.VerifyAnalyzerAsync(@" -Imports System -Public Class C - Public Sub Test(p1 As String, Optional p2 As String = Nothing) - Throw New NotImplementedException() - End Sub -End Class"); - } - - [Fact] - [WorkItem(2589, "https://github.com/dotnet/roslyn-analyzers/issues/2589")] - [WorkItem(2593, "https://github.com/dotnet/roslyn-analyzers/issues/2593")] - public async Task NoDiagnosticDiscardParameterNames() - { - await VerifyCS.VerifyAnalyzerAsync(@" -using System; - -public class C -{ - public void M(int _, int _1, int _4) - { - } -} -"); - - await VerifyVB.VerifyAnalyzerAsync(@" -Imports System - -Public Class C - ' _ is not an allowed identifier in VB. - Public Sub M(_1 As Integer, _2 As Integer, _4 As Integer) - End Sub -End Class -"); - } - - [Fact] - [WorkItem(2466, "https://github.com/dotnet/roslyn-analyzers/issues/2466")] - public async Task NoDiagnosticUsedLocalFunctionParameters() - { - await VerifyCS.VerifyAnalyzerAsync(@" -using System; - -public class C -{ - public void M() - { - LocalFunction(0); - return; - - void LocalFunction(int x) - { - Console.WriteLine(x); - } - } -} -"); - } - - [Theory] - [WorkItem(1375, "https://github.com/dotnet/roslyn-analyzers/issues/1375")] - [InlineData("public", "dotnet_code_quality.api_surface = private", false)] - [InlineData("private", "dotnet_code_quality.api_surface = internal, public", false)] - [InlineData("public", "dotnet_code_quality.CA1801.api_surface = internal, private", false)] - [InlineData("public", "dotnet_code_quality.CA1801.api_surface = Friend, Private", false)] - [InlineData("public", "dotnet_code_quality.Usage.api_surface = internal, private", false)] - [InlineData("public", @"dotnet_code_quality.api_surface = all - dotnet_code_quality.CA1801.api_surface = private", false)] - [InlineData("public", "dotnet_code_quality.api_surface = public", true)] - [InlineData("public", "dotnet_code_quality.api_surface = internal, public", true)] - [InlineData("public", "dotnet_code_quality.CA1801.api_surface = public", true)] - [InlineData("public", "dotnet_code_quality.CA1801.api_surface = all", true)] - [InlineData("public", "dotnet_code_quality.Usage.api_surface = public, private", true)] - [InlineData("public", @"dotnet_code_quality.api_surface = all - dotnet_code_quality.CA1801.api_surface = public", true)] - public async Task EditorConfigConfiguration_ApiSurfaceOption_AsAdditionalDocument(string accessibility, string editorConfigText, bool expectDiagnostic) - { - var paramName = expectDiagnostic ? "[|unused|]" : "unused"; - - await new VerifyCS.Test - { - TestState = - { - Sources = - { - $@" -public class C -{{ - {accessibility} void M(int {paramName}) - {{ - }} -}}" - }, - AdditionalFiles = { (".editorconfig", editorConfigText) } - } - }.RunAsync(); - - await new VerifyVB.Test - { - TestState = - { - Sources = - { - $@" -Public Class C - {accessibility} Sub M({paramName} As Integer) - End Sub -End Class" - }, - AdditionalFiles = { (".editorconfig", editorConfigText) } - } - }.RunAsync(); - } - - [Theory] - [WorkItem(1375, "https://github.com/dotnet/roslyn-analyzers/issues/1375")] - [InlineData("public", "dotnet_code_quality.api_surface = private", false)] - [InlineData("private", "dotnet_code_quality.api_surface = internal, public", false)] - [InlineData("public", "dotnet_code_quality.CA1801.api_surface = internal, private", false)] - [InlineData("public", "dotnet_code_quality.CA1801.api_surface = Friend, Private", false)] - [InlineData("public", "dotnet_code_quality.Usage.api_surface = internal, private", false)] - [InlineData("public", @"dotnet_code_quality.api_surface = all - dotnet_code_quality.CA1801.api_surface = private", false)] - [InlineData("public", "dotnet_code_quality.api_surface = public", true)] - [InlineData("public", "dotnet_code_quality.api_surface = internal, public", true)] - [InlineData("public", "dotnet_code_quality.CA1801.api_surface = public", true)] - [InlineData("public", "dotnet_code_quality.CA1801.api_surface = all", true)] - [InlineData("public", "dotnet_code_quality.Usage.api_surface = public, private", true)] - [InlineData("public", @"dotnet_code_quality.api_surface = all - dotnet_code_quality.CA1801.api_surface = public", true)] - public async Task EditorConfigConfiguration_ApiSurfaceOption_AsAnalyzerConfigDocument(string accessibility, string editorConfigText, bool expectDiagnostic) - { - var csTest = new VerifyCS.Test - { - TestState = - { - Sources = - { - $@" -public class C -{{ - {accessibility} void M(int unused) - {{ - }} -}}" - } - }, - SolutionTransforms = { WithAnalyzerConfigDocument } - }; - - if (expectDiagnostic) - { - csTest.ExpectedDiagnostics.Add(VerifyCS.Diagnostic().WithSpan(@"z:\Test0.cs", 4, 23, 4, 29).WithArguments("unused", "M")); - } - - await csTest.RunAsync(); - - var vbTest = new VerifyVB.Test - { - TestState = - { - Sources = - { - $@" -Public Class C - {accessibility} Sub M(unused As Integer) - End Sub -End Class" - }, - }, - SolutionTransforms = { WithAnalyzerConfigDocument } - }; - - if (expectDiagnostic) - { - vbTest.ExpectedDiagnostics.Add(VerifyVB.Diagnostic().WithSpan(@"z:\Test0.vb", 3, 18, 3, 24).WithArguments("unused", "M")); - } - - await vbTest.RunAsync(); - return; - - Solution WithAnalyzerConfigDocument(Solution solution, ProjectId projectId) - { - var project = solution.GetProject(projectId)!; - var projectFilePath = project.Language == LanguageNames.CSharp ? @"z:\Test.csproj" : @"z:\Test.vbproj"; - solution = solution.WithProjectFilePath(projectId, projectFilePath); - - var documentId = project.DocumentIds.Single(); - var documentExtension = project.Language == LanguageNames.CSharp ? "cs" : "vb"; - solution = solution.WithDocumentFilePath(documentId, $@"z:\Test0.{documentExtension}"); - - return solution.GetProject(projectId)! - .AddAnalyzerConfigDocument( - ".editorconfig", - SourceText.From($"[*.{documentExtension}]" + Environment.NewLine + editorConfigText), - filePath: @"z:\.editorconfig") - .Project.Solution; - } - } - - [Theory] - [WorkItem(1375, "https://github.com/dotnet/roslyn-analyzers/issues/1375")] - [InlineData("public", "dotnet_code_quality.api_surface = private", false)] - [InlineData("private", "dotnet_code_quality.api_surface = internal, public", false)] - [InlineData("public", "dotnet_code_quality.CA1801.api_surface = internal, private", false)] - [InlineData("public", "dotnet_code_quality.CA1801.api_surface = Friend, Private", false)] - [InlineData("public", "dotnet_code_quality.Usage.api_surface = internal, private", false)] - [InlineData("public", @"dotnet_code_quality.api_surface = all - dotnet_code_quality.CA1801.api_surface = private", false)] - [InlineData("public", "dotnet_code_quality.api_surface = public", true)] - [InlineData("public", "dotnet_code_quality.api_surface = internal, public", true)] - [InlineData("public", "dotnet_code_quality.CA1801.api_surface = public", true)] - [InlineData("public", "dotnet_code_quality.CA1801.api_surface = all", true)] - [InlineData("public", "dotnet_code_quality.Usage.api_surface = public, private", true)] - [InlineData("public", @"dotnet_code_quality.api_surface = all - dotnet_code_quality.CA1801.api_surface = public", true)] - public async Task EditorConfigConfiguration_ApiSurfaceOption_AsAnalyzerConfigDocumentAndAdditionalDocument(string accessibility, string editorConfigText, bool expectDiagnostic) - { - var csTest = new VerifyCS.Test - { - TestState = - { - Sources = - { - $@" -public class C -{{ - {accessibility} void M(int unused) - {{ - }} -}}" - }, - AdditionalFiles = { (".editorconfig", editorConfigText) } - }, - SolutionTransforms = { WithAnalyzerConfigDocument } - }; - - if (expectDiagnostic) - { - csTest.ExpectedDiagnostics.Add(VerifyCS.Diagnostic().WithSpan(@"z:\Test0.cs", 4, 23, 4, 29).WithArguments("unused", "M")); - } - - await csTest.RunAsync(); - - var vbTest = new VerifyVB.Test - { - TestState = - { - Sources = - { - $@" -Public Class C - {accessibility} Sub M(unused As Integer) - End Sub -End Class" - }, - AdditionalFiles = { (".editorconfig", editorConfigText) } - }, - SolutionTransforms = { WithAnalyzerConfigDocument } - }; - - if (expectDiagnostic) - { - vbTest.ExpectedDiagnostics.Add(VerifyVB.Diagnostic().WithSpan(@"z:\Test0.vb", 3, 18, 3, 24).WithArguments("unused", "M")); - } - - await vbTest.RunAsync(); - return; - - Solution WithAnalyzerConfigDocument(Solution solution, ProjectId projectId) - { - var project = solution.GetProject(projectId)!; - var projectFilePath = project.Language == LanguageNames.CSharp ? @"z:\Test.csproj" : @"z:\Test.vbproj"; - solution = solution.WithProjectFilePath(projectId, projectFilePath); - - var documentId = project.DocumentIds.Single(); - var documentExtension = project.Language == LanguageNames.CSharp ? "cs" : "vb"; - solution = solution.WithDocumentFilePath(documentId, $@"z:\Test0.{documentExtension}"); - - return solution.GetProject(projectId)! - .AddAnalyzerConfigDocument( - ".editorconfig", - SourceText.From($"[*.{documentExtension}]" + Environment.NewLine + editorConfigText), - filePath: @"z:\.editorconfig") - .Project.Solution; - } - } - - [Fact, WorkItem(3106, "https://github.com/dotnet/roslyn-analyzers/issues/3106")] - public async Task EventArgsNotInheritingFromSystemEventArgs_NoDiagnostic() - { - await VerifyCS.VerifyAnalyzerAsync(@" -// Reproduce UWP some specific EventArgs -namespace Windows.UI.Xaml -{ - public class RoutedEventArgs {} - public class SizeChangedEventArgs : RoutedEventArgs {} - public class WindowCreatedEventArgs {} -} - -namespace SomeNamespace -{ - public class MyCustomEventArgs {} -} - -public class C -{ - private void Page_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e) {} - private void OnSizeChanged(object sender, Windows.UI.Xaml.SizeChangedEventArgs e) {} - private void OnWindowCreated(object sender, Windows.UI.Xaml.WindowCreatedEventArgs e) {} - - private void OnSomething(object sender, SomeNamespace.MyCustomEventArgs e) {} -}"); - - await VerifyVB.VerifyAnalyzerAsync(@" -' Reproduce UWP some specific EventArgs -Namespace Windows.UI.Xaml - Public Class RoutedEventArgs - End Class - - Public Class SizeChangedEventArgs - Inherits RoutedEventArgs - End Class - - Public Class WindowCreatedEventArgs - End Class -End Namespace - -Namespace SomeNamespace - Public Class MyCustomEventArgs - End Class -End Namespace - -Public Class C - Private Sub Page_Loaded(ByVal sender As Object, ByVal e As Windows.UI.Xaml.RoutedEventArgs) - End Sub - - Private Sub OnSizeChanged(ByVal sender As Object, ByVal e As Windows.UI.Xaml.SizeChangedEventArgs) - End Sub - - Private Sub OnWindowCreated(ByVal sender As Object, ByVal e As Windows.UI.Xaml.WindowCreatedEventArgs) - End Sub - - Private Sub OnSomething(ByVal sender As Object, ByVal e As SomeNamespace.MyCustomEventArgs) - End Sub -End Class"); - } - - [Fact, WorkItem(3039, "https://github.com/dotnet/roslyn-analyzers/issues/3039")] - public async Task SerializationConstructorParameters_NoDiagnostic() - { - await VerifyCS.VerifyAnalyzerAsync(@" -using System; -using System.Runtime.Serialization; - -public class C -{ - protected C(SerializationInfo info, StreamingContext context) - { - } -}"); - - await VerifyVB.VerifyAnalyzerAsync(@" -Imports System.Runtime.Serialization - -Public Class C - Protected Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext) - End Sub -End Class"); - } - - [Fact, WorkItem(3039, "https://github.com/dotnet/roslyn-analyzers/issues/3039")] - public async Task GetObjectDataParameters_NoDiagnostic() - { - await VerifyCS.VerifyAnalyzerAsync(@" -using System; -using System.Runtime.Serialization; - -public class C -{ - public void GetObjectData(SerializationInfo info, StreamingContext context) - { - } -}"); - - await VerifyVB.VerifyAnalyzerAsync(@" -Imports System.Runtime.Serialization - -Public Class C - Public Sub GetObjectData(ByVal info As SerializationInfo, ByVal context As StreamingContext) - End Sub -End Class"); - } - - [Fact] - [WorkItem(2846, "https://github.com/dotnet/roslyn-analyzers/issues/2846")] - public async Task CA1801_MethodThrowArrowExpression_NoDiagnostic() - { - await VerifyCS.VerifyAnalyzerAsync(@" -using System; - -public class Class1 -{ - public int Method1(int value) => throw new NotImplementedException(); -} -"); - } - - [Fact, WorkItem(4052, "https://github.com/dotnet/roslyn-analyzers/issues/4052")] - public async Task CA1801_TopLevelStatements_NoDiagnostic() - { - await new VerifyCS.Test() - { - TestCode = @"int x = 0;", - LanguageVersion = CodeAnalysis.CSharp.LanguageVersion.CSharp9, - SolutionTransforms = - { - (solution, projectId) => - { - var project = solution.GetProject(projectId); - project = project.WithCompilationOptions(project.CompilationOptions.WithOutputKind(OutputKind.ConsoleApplication)); - return project.Solution; - }, - } - }.RunAsync(); - } - - [Fact, WorkItem(4462, "https://github.com/dotnet/roslyn-analyzers/issues/4462")] - public async Task CA1801_CSharp_ImplicitRecord() - { - await new VerifyCS.Test - { - LanguageVersion = CodeAnalysis.CSharp.LanguageVersion.CSharp9, - ReferenceAssemblies = ReferenceAssemblies.Net.Net50, - TestCode = @" -public record Person(string Name, int Age = 0); - -public record Person2(string Name, int Age = 0) {}", - }.RunAsync(); - } - - #endregion - - #region Unit tests for analyzer diagnostic(s) - - [Fact] - [WorkItem(459, "https://github.com/dotnet/roslyn-analyzers/issues/459")] - public async Task CSharp_DiagnosticForSimpleCasesTest() - { - await VerifyCS.VerifyAnalyzerAsync(@" -using System; - -class C -{ - public C(int param) - { - } - - public void UnusedParamMethod(int param) - { - } - - public static void UnusedParamStaticMethod(int param1) - { - } - - public void UnusedDefaultParamMethod(int defaultParam = 1) - { - } - - public void UnusedParamsArrayParamMethod(params int[] paramsArr) - { - } - - public void MultipleUnusedParamsMethod(int param1, int param2) - { - } - - private void UnusedRefParamMethod(ref int param1) - { - } - - public void UnusedErrorTypeParamMethod({|CS0246:UndefinedType|} param1) - { - } -} -", - GetCSharpUnusedParameterResultAt(6, 18, "param", ".ctor"), - GetCSharpUnusedParameterResultAt(10, 39, "param", "UnusedParamMethod"), - GetCSharpUnusedParameterResultAt(14, 52, "param1", "UnusedParamStaticMethod"), - GetCSharpUnusedParameterResultAt(18, 46, "defaultParam", "UnusedDefaultParamMethod"), - GetCSharpUnusedParameterResultAt(22, 59, "paramsArr", "UnusedParamsArrayParamMethod"), - GetCSharpUnusedParameterResultAt(26, 48, "param1", "MultipleUnusedParamsMethod"), - GetCSharpUnusedParameterResultAt(26, 60, "param2", "MultipleUnusedParamsMethod"), - GetCSharpUnusedParameterResultAt(30, 47, "param1", "UnusedRefParamMethod"), - GetCSharpUnusedParameterResultAt(34, 58, "param1", "UnusedErrorTypeParamMethod")); - } - - [Fact] - [WorkItem(459, "https://github.com/dotnet/roslyn-analyzers/issues/459")] - public async Task Basic_DiagnosticForSimpleCasesTest() - { - await VerifyVB.VerifyAnalyzerAsync(@" -Class C - Public Sub New(param As Integer) - End Sub - - Public Sub UnusedParamMethod(param As Integer) - End Sub - - Public Shared Sub UnusedParamStaticMethod(param1 As Integer) - End Sub - - Public Sub UnusedDefaultParamMethod(Optional defaultParam As Integer = 1) - End Sub - - Public Sub UnusedParamsArrayParamMethod(ParamArray paramsArr As Integer()) - End Sub - - Public Sub MultipleUnusedParamsMethod(param1 As Integer, param2 As Integer) - End Sub - - Private Sub UnusedRefParamMethod(ByRef param1 As Integer) - End Sub - - Public Sub UnusedErrorTypeParamMethod(param1 As {|BC30002:UndefinedType|}) - End Sub -End Class -", - GetBasicUnusedParameterResultAt(3, 20, "param", ".ctor"), - GetBasicUnusedParameterResultAt(6, 34, "param", "UnusedParamMethod"), - GetBasicUnusedParameterResultAt(9, 47, "param1", "UnusedParamStaticMethod"), - GetBasicUnusedParameterResultAt(12, 50, "defaultParam", "UnusedDefaultParamMethod"), - GetBasicUnusedParameterResultAt(15, 56, "paramsArr", "UnusedParamsArrayParamMethod"), - GetBasicUnusedParameterResultAt(18, 43, "param1", "MultipleUnusedParamsMethod"), - GetBasicUnusedParameterResultAt(18, 62, "param2", "MultipleUnusedParamsMethod"), - GetBasicUnusedParameterResultAt(21, 44, "param1", "UnusedRefParamMethod"), - GetBasicUnusedParameterResultAt(24, 43, "param1", "UnusedErrorTypeParamMethod")); - } - - [Fact] - public async Task DiagnosticsForNonFirstParameterOfExtensionMethod() - { - await VerifyCS.VerifyAnalyzerAsync(@" -static class C -{ - static void ExtensionMethod(this int i, int anotherParam) { } -} -", - GetCSharpUnusedParameterResultAt(4, 49, "anotherParam", "ExtensionMethod")); - } - - [Fact] - [WorkItem(2466, "https://github.com/dotnet/roslyn-analyzers/issues/2466")] - public async Task DiagnosticForUnusedLocalFunctionParameters_01() - { - await VerifyCS.VerifyAnalyzerAsync(@" -using System; - -public class C -{ - public void M() - { - LocalFunction(0); - return; - - void LocalFunction(int x) - { - } - } -}", - GetCSharpUnusedParameterResultAt(11, 32, "x", "LocalFunction")); - } - - [Fact] - [WorkItem(2466, "https://github.com/dotnet/roslyn-analyzers/issues/2466")] - public async Task DiagnosticForUnusedLocalFunctionParameters_02() - { - await VerifyCS.VerifyAnalyzerAsync(@" -using System; - -public class C -{ - public void M() - { - // Flag unused parameter even if LocalFunction is unused. - void LocalFunction(int x) - { - } - } -}", - GetCSharpUnusedParameterResultAt(9, 32, "x", "LocalFunction")); - } - - [Fact] - public async Task DiagnosticForMethodsInNestedTypes() - { - await VerifyCS.VerifyAnalyzerAsync(@" -using System; - -public class C -{ - public void OuterM(int [|x|]) - { - } - - public class NestedType - { - public void InnerM(int [|y|]) - { - } - } -}"); - } - - [Fact, WorkItem(4462, "https://github.com/dotnet/roslyn-analyzers/issues/4462")] - public async Task CA1801_CSharp_Record() - { - await new VerifyCS.Test - { - LanguageVersion = CodeAnalysis.CSharp.LanguageVersion.CSharp9, - ReferenceAssemblies = ReferenceAssemblies.Net.Net50, - TestCode = @" -public record OtherPerson -{ - public string Name { get; init; } - - public OtherPerson(string name, int [|age|] = 0) - => Name = name; -}", - }.RunAsync(); - } - - #endregion - - #region Helpers - - private static DiagnosticResult GetCSharpUnusedParameterResultAt(int line, int column, string parameterName, string methodName) - => VerifyCS.Diagnostic() - .WithLocation(line, column) - .WithArguments(parameterName, methodName); - - private static DiagnosticResult GetBasicUnusedParameterResultAt(int line, int column, string parameterName, string methodName) - => VerifyVB.Diagnostic() - .WithLocation(line, column) - .WithArguments(parameterName, methodName); - - #endregion - } -} diff --git a/src/NetAnalyzers/VisualBasic/Microsoft.CodeQuality.Analyzers/Maintainability/BasicReviewUnusedParameters.Fixer.vb b/src/NetAnalyzers/VisualBasic/Microsoft.CodeQuality.Analyzers/Maintainability/BasicReviewUnusedParameters.Fixer.vb deleted file mode 100644 index f25fb1b220..0000000000 --- a/src/NetAnalyzers/VisualBasic/Microsoft.CodeQuality.Analyzers/Maintainability/BasicReviewUnusedParameters.Fixer.vb +++ /dev/null @@ -1,26 +0,0 @@ -' Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -Imports System.Composition -Imports Microsoft.CodeAnalysis -Imports Microsoft.CodeAnalysis.CodeFixes -Imports Microsoft.CodeAnalysis.VisualBasic -Imports Microsoft.CodeQuality.Analyzers.Maintainability - -Namespace Microsoft.CodeQuality.VisualBasic.Analyzers.Maintainability - ''' - ''' CA1801: Review unused parameters - ''' - - Public NotInheritable Class BasicReviewUnusedParametersFixer - Inherits ReviewUnusedParametersFixer - - Protected Overrides Function GetParameterDeclarationNode(node As SyntaxNode) As SyntaxNode - Return node.Parent - End Function - - Protected Overrides Function CanContinuouslyLeadToObjectCreationOrInvocation(node As SyntaxNode) As Boolean - Dim kind = node.Kind() - Return kind = SyntaxKind.QualifiedName OrElse kind = SyntaxKind.IdentifierName OrElse kind = SyntaxKind.SimpleMemberAccessExpression - End Function - End Class -End Namespace \ No newline at end of file diff --git a/src/NetAnalyzers/VisualBasic/Microsoft.CodeQuality.Analyzers/Maintainability/BasicReviewUnusedParametersAnalyzer.vb b/src/NetAnalyzers/VisualBasic/Microsoft.CodeQuality.Analyzers/Maintainability/BasicReviewUnusedParametersAnalyzer.vb deleted file mode 100644 index 8aacd1236f..0000000000 --- a/src/NetAnalyzers/VisualBasic/Microsoft.CodeQuality.Analyzers/Maintainability/BasicReviewUnusedParametersAnalyzer.vb +++ /dev/null @@ -1,20 +0,0 @@ -' Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -Imports Microsoft.CodeAnalysis -Imports Microsoft.CodeAnalysis.Diagnostics -Imports Microsoft.CodeQuality.Analyzers.Maintainability - -Namespace Microsoft.CodeQuality.VisualBasic.Analyzers.Maintainability - - ''' - ''' CA1801: Review unused parameters - ''' - - Public NotInheritable Class BasicReviewUnusedParametersAnalyzer - Inherits ReviewUnusedParametersAnalyzer - - Protected Overrides Function IsPositionalRecordPrimaryConstructor(methodSymbol As IMethodSymbol) As Boolean - Return False - End Function - End Class -End Namespace \ No newline at end of file From 1c852e3a69d5c8a60ebe599d7962b8efd16d9bf0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaury=20Lev=C3=A9?= Date: Wed, 9 Dec 2020 11:01:18 +0100 Subject: [PATCH 3/6] Add CA1801 to removed rules list --- src/NetAnalyzers/Core/AnalyzerReleases.Unshipped.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/NetAnalyzers/Core/AnalyzerReleases.Unshipped.md b/src/NetAnalyzers/Core/AnalyzerReleases.Unshipped.md index cdf4f1397e..f7dbd19c06 100644 --- a/src/NetAnalyzers/Core/AnalyzerReleases.Unshipped.md +++ b/src/NetAnalyzers/Core/AnalyzerReleases.Unshipped.md @@ -1 +1,5 @@ -; Please do not edit this file manually, it should only be updated through code fix application. +### Removed Rules + +Rule ID | Category | Severity | Notes +--------|----------|----------|------- +CA1801 | Usage | Disabled | ReviewUnusedParametersAnalyzer, [Documentation](https://docs.microsoft.com/visualstudio/code-quality/ca1801) From 7f39ec87ebcbef5130c5efff84b8cae9c015f66b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaury=20Lev=C3=A9?= Date: Wed, 9 Dec 2020 12:08:50 +0100 Subject: [PATCH 4/6] Fix md and sarif files --- .../Microsoft.CodeAnalysis.FxCopAnalyzers.md | 12 ------ ...icrosoft.CodeAnalysis.FxCopAnalyzers.sarif | 40 ------------------- .../Microsoft.CodeQuality.Analyzers.md | 12 ------ .../Microsoft.CodeQuality.Analyzers.sarif | 40 ------------------- 4 files changed, 104 deletions(-) diff --git a/src/Microsoft.CodeAnalysis.FxCopAnalyzers/Microsoft.CodeAnalysis.FxCopAnalyzers.md b/src/Microsoft.CodeAnalysis.FxCopAnalyzers/Microsoft.CodeAnalysis.FxCopAnalyzers.md index 626f4037ca..ae4484b509 100644 --- a/src/Microsoft.CodeAnalysis.FxCopAnalyzers/Microsoft.CodeAnalysis.FxCopAnalyzers.md +++ b/src/Microsoft.CodeAnalysis.FxCopAnalyzers/Microsoft.CodeAnalysis.FxCopAnalyzers.md @@ -972,18 +972,6 @@ Consistent naming of parameters in an override hierarchy increases the usability |CodeFix|True| --- -## [CA1801](https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1801): Review unused parameters - -Avoid unused paramereters in your code. If the parameter cannot be removed, then change its name so it starts with an underscore and is optionally followed by an integer, such as '_', '_1', '_2', etc. These are treated as special discard symbol names. - -|Item|Value| -|-|-| -|Category|Usage| -|Enabled|True| -|Severity|Warning| -|CodeFix|True| ---- - ## [CA1802](https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1802): Use literals where appropriate A field is declared static and read-only (Shared and ReadOnly in Visual Basic), and is initialized by using a value that is computable at compile time. Because the value that is assigned to the targeted field is computable at compile time, change the declaration to a const (Const in Visual Basic) field so that the value is computed at compile time instead of at run?time. diff --git a/src/Microsoft.CodeAnalysis.FxCopAnalyzers/Microsoft.CodeAnalysis.FxCopAnalyzers.sarif b/src/Microsoft.CodeAnalysis.FxCopAnalyzers/Microsoft.CodeAnalysis.FxCopAnalyzers.sarif index ed533befff..b3a5eca27e 100644 --- a/src/Microsoft.CodeAnalysis.FxCopAnalyzers/Microsoft.CodeAnalysis.FxCopAnalyzers.sarif +++ b/src/Microsoft.CodeAnalysis.FxCopAnalyzers/Microsoft.CodeAnalysis.FxCopAnalyzers.sarif @@ -2031,26 +2031,6 @@ ] } }, - "CA1801": { - "id": "CA1801", - "shortDescription": "Review unused parameters", - "fullDescription": "Avoid unused paramereters in your code. If the parameter cannot be removed, then change its name so it starts with an underscore and is optionally followed by an integer, such as '_', '_1', '_2', etc. These are treated as special discard symbol names.", - "defaultLevel": "warning", - "helpUri": "https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1801", - "properties": { - "category": "Usage", - "isEnabledByDefault": true, - "typeName": "CSharpReviewUnusedParametersAnalyzer", - "languages": [ - "C#" - ], - "tags": [ - "PortedFromFxCop", - "Telemetry", - "EnabledRuleInAggressiveMode" - ] - } - }, "CA1812": { "id": "CA1812", "shortDescription": "Avoid uninstantiated internal classes", @@ -2219,26 +2199,6 @@ ] } }, - "CA1801": { - "id": "CA1801", - "shortDescription": "Review unused parameters", - "fullDescription": "Avoid unused paramereters in your code. If the parameter cannot be removed, then change its name so it starts with an underscore and is optionally followed by an integer, such as '_', '_1', '_2', etc. These are treated as special discard symbol names.", - "defaultLevel": "warning", - "helpUri": "https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1801", - "properties": { - "category": "Usage", - "isEnabledByDefault": true, - "typeName": "BasicReviewUnusedParametersAnalyzer", - "languages": [ - "Visual Basic" - ], - "tags": [ - "PortedFromFxCop", - "Telemetry", - "EnabledRuleInAggressiveMode" - ] - } - }, "CA1812": { "id": "CA1812", "shortDescription": "Avoid uninstantiated internal classes", diff --git a/src/Microsoft.CodeQuality.Analyzers/Microsoft.CodeQuality.Analyzers.md b/src/Microsoft.CodeQuality.Analyzers/Microsoft.CodeQuality.Analyzers.md index 4a3acc7b11..d26098b3ca 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Microsoft.CodeQuality.Analyzers.md +++ b/src/Microsoft.CodeQuality.Analyzers/Microsoft.CodeQuality.Analyzers.md @@ -840,18 +840,6 @@ Consistent naming of parameters in an override hierarchy increases the usability |CodeFix|True| --- -## [CA1801](https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1801): Review unused parameters - -Avoid unused paramereters in your code. If the parameter cannot be removed, then change its name so it starts with an underscore and is optionally followed by an integer, such as '_', '_1', '_2', etc. These are treated as special discard symbol names. - -|Item|Value| -|-|-| -|Category|Usage| -|Enabled|True| -|Severity|Warning| -|CodeFix|True| ---- - ## [CA1802](https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1802): Use literals where appropriate A field is declared static and read-only (Shared and ReadOnly in Visual Basic), and is initialized by using a value that is computable at compile time. Because the value that is assigned to the targeted field is computable at compile time, change the declaration to a const (Const in Visual Basic) field so that the value is computed at compile time instead of at run?time. diff --git a/src/Microsoft.CodeQuality.Analyzers/Microsoft.CodeQuality.Analyzers.sarif b/src/Microsoft.CodeQuality.Analyzers/Microsoft.CodeQuality.Analyzers.sarif index 1385dd213f..09971cd0af 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Microsoft.CodeQuality.Analyzers.sarif +++ b/src/Microsoft.CodeQuality.Analyzers/Microsoft.CodeQuality.Analyzers.sarif @@ -2016,26 +2016,6 @@ ] } }, - "CA1801": { - "id": "CA1801", - "shortDescription": "Review unused parameters", - "fullDescription": "Avoid unused paramereters in your code. If the parameter cannot be removed, then change its name so it starts with an underscore and is optionally followed by an integer, such as '_', '_1', '_2', etc. These are treated as special discard symbol names.", - "defaultLevel": "warning", - "helpUri": "https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1801", - "properties": { - "category": "Usage", - "isEnabledByDefault": true, - "typeName": "CSharpReviewUnusedParametersAnalyzer", - "languages": [ - "C#" - ], - "tags": [ - "PortedFromFxCop", - "Telemetry", - "EnabledRuleInAggressiveMode" - ] - } - }, "CA1812": { "id": "CA1812", "shortDescription": "Avoid uninstantiated internal classes", @@ -2204,26 +2184,6 @@ ] } }, - "CA1801": { - "id": "CA1801", - "shortDescription": "Review unused parameters", - "fullDescription": "Avoid unused paramereters in your code. If the parameter cannot be removed, then change its name so it starts with an underscore and is optionally followed by an integer, such as '_', '_1', '_2', etc. These are treated as special discard symbol names.", - "defaultLevel": "warning", - "helpUri": "https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1801", - "properties": { - "category": "Usage", - "isEnabledByDefault": true, - "typeName": "BasicReviewUnusedParametersAnalyzer", - "languages": [ - "Visual Basic" - ], - "tags": [ - "PortedFromFxCop", - "Telemetry", - "EnabledRuleInAggressiveMode" - ] - } - }, "CA1812": { "id": "CA1812", "shortDescription": "Avoid uninstantiated internal classes", From 202c1edb60ff56688fbd6a214ab7e3c26d3ef519 Mon Sep 17 00:00:00 2001 From: Mateo Torres Ruiz Date: Fri, 8 Jan 2021 14:29:08 -0800 Subject: [PATCH 5/6] Remove single-file analyzer --- .../Core/AnalyzerReleases.Unshipped.md | 8 +- .../MicrosoftNetCoreAnalyzersResources.resx | 9 - .../AvoidAssemblyLocationInSingleFile.cs | 161 ----------------- .../MicrosoftNetCoreAnalyzersResources.cs.xlf | 15 -- .../MicrosoftNetCoreAnalyzersResources.de.xlf | 15 -- .../MicrosoftNetCoreAnalyzersResources.es.xlf | 15 -- .../MicrosoftNetCoreAnalyzersResources.fr.xlf | 15 -- .../MicrosoftNetCoreAnalyzersResources.it.xlf | 15 -- .../MicrosoftNetCoreAnalyzersResources.ja.xlf | 15 -- .../MicrosoftNetCoreAnalyzersResources.ko.xlf | 15 -- .../MicrosoftNetCoreAnalyzersResources.pl.xlf | 15 -- ...crosoftNetCoreAnalyzersResources.pt-BR.xlf | 15 -- .../MicrosoftNetCoreAnalyzersResources.ru.xlf | 15 -- .../MicrosoftNetCoreAnalyzersResources.tr.xlf | 15 -- ...osoftNetCoreAnalyzersResources.zh-Hans.xlf | 15 -- ...osoftNetCoreAnalyzersResources.zh-Hant.xlf | 15 -- .../Microsoft.CodeAnalysis.NetAnalyzers.md | 24 --- .../Microsoft.CodeAnalysis.NetAnalyzers.sarif | 40 ----- .../AvoidAssemblyLocationInSingleFileTests.cs | 167 ------------------ src/Utilities/Compiler/DiagnosticCategory.cs | 1 - .../DiagnosticCategoryAndIdRanges.txt | 6 +- .../Options/MSBuildPropertyOptionNames.cs | 2 - 22 files changed, 8 insertions(+), 605 deletions(-) delete mode 100644 src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Publish/AvoidAssemblyLocationInSingleFile.cs delete mode 100644 src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Publish/AvoidAssemblyLocationInSingleFileTests.cs diff --git a/src/NetAnalyzers/Core/AnalyzerReleases.Unshipped.md b/src/NetAnalyzers/Core/AnalyzerReleases.Unshipped.md index 0058139817..5f3c6ac8fb 100644 --- a/src/NetAnalyzers/Core/AnalyzerReleases.Unshipped.md +++ b/src/NetAnalyzers/Core/AnalyzerReleases.Unshipped.md @@ -1 +1,7 @@ -; Please do not edit this file manually, it should only be updated through code fix application. \ No newline at end of file +; Please do not edit this file manually, it should only be updated through code fix application. + +### Removed Rules +Rule ID | Category | Severity | Notes +--------|----------|----------|------- +IL3000 | Public | Warning | Moved analyzer to mono/linker +IL3001 | Public | Warning | Moved analyzer to mono/linker \ No newline at end of file diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx index e240378b84..0b8e62a9ba 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx @@ -1463,15 +1463,6 @@ Avoid 'StringBuilder' parameters for P/Invokes - - Avoid using accessing Assembly file path when publishing as a single-file - - - '{0}' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. - - - '{0}' will throw for assemblies embedded in a single-file app - This call site is reachable on: {2}. '{0}' is only supported on: {1}. This call site is reachable on: 'windows' all versions.'SupportedOnWindowsUnsupportedFromWindows2004()' is only supported on: 'windows' 10.0.2004 and before diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Publish/AvoidAssemblyLocationInSingleFile.cs b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Publish/AvoidAssemblyLocationInSingleFile.cs deleted file mode 100644 index 505f561c5c..0000000000 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Publish/AvoidAssemblyLocationInSingleFile.cs +++ /dev/null @@ -1,161 +0,0 @@ -// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Collections.Generic; -using System.Collections.Immutable; -using Analyzer.Utilities; -using Analyzer.Utilities.Extensions; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.Diagnostics; -using Microsoft.CodeAnalysis.Operations; - -namespace Microsoft.NetCore.Analyzers.Publish -{ - /// - /// IL3000, IL3001: Do not use Assembly file path in single-file publish - /// - [DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)] - public sealed class AvoidAssemblyLocationInSingleFile : DiagnosticAnalyzer - { - public const string IL3000 = nameof(IL3000); - public const string IL3001 = nameof(IL3001); - - internal static DiagnosticDescriptor LocationRule = DiagnosticDescriptorHelper.Create( - IL3000, - new LocalizableResourceString(nameof(MicrosoftNetCoreAnalyzersResources.AvoidAssemblyLocationInSingleFileTitle), - MicrosoftNetCoreAnalyzersResources.ResourceManager, typeof(MicrosoftNetCoreAnalyzersResources)), - new LocalizableResourceString(nameof(MicrosoftNetCoreAnalyzersResources.AvoidAssemblyLocationInSingleFileMessage), - MicrosoftNetCoreAnalyzersResources.ResourceManager, typeof(MicrosoftNetCoreAnalyzersResources)), - DiagnosticCategory.Publish, - RuleLevel.BuildWarning, - description: null, - isPortedFxCopRule: false, - isDataflowRule: false); - - internal static DiagnosticDescriptor GetFilesRule = DiagnosticDescriptorHelper.Create( - IL3001, - new LocalizableResourceString(nameof(MicrosoftNetCoreAnalyzersResources.AvoidAssemblyLocationInSingleFileTitle), - MicrosoftNetCoreAnalyzersResources.ResourceManager, typeof(MicrosoftNetCoreAnalyzersResources)), - new LocalizableResourceString(nameof(MicrosoftNetCoreAnalyzersResources.AvoidAssemblyGetFilesInSingleFileMessage), - MicrosoftNetCoreAnalyzersResources.ResourceManager, typeof(MicrosoftNetCoreAnalyzersResources)), - DiagnosticCategory.Publish, - RuleLevel.BuildWarning, - description: null, - isPortedFxCopRule: false, - isDataflowRule: false); - - public override ImmutableArray SupportedDiagnostics => ImmutableArray.Create(LocationRule, GetFilesRule); - - public override void Initialize(AnalysisContext context) - { - context.EnableConcurrentExecution(); - context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.ReportDiagnostics); - - context.RegisterCompilationStartAction(context => - { - var compilation = context.Compilation; - var isSingleFilePublish = context.Options.GetMSBuildPropertyValue( - MSBuildPropertyOptionNames.PublishSingleFile, compilation, context.CancellationToken); - if (!string.Equals(isSingleFilePublish?.Trim(), "true", StringComparison.OrdinalIgnoreCase)) - { - return; - } - var includesAllContent = context.Options.GetMSBuildPropertyValue( - MSBuildPropertyOptionNames.IncludeAllContentForSelfExtract, compilation, context.CancellationToken); - if (string.Equals(includesAllContent?.Trim(), "true", StringComparison.OrdinalIgnoreCase)) - { - return; - } - - var propertiesBuilder = ImmutableArray.CreateBuilder(); - var methodsBuilder = ImmutableArray.CreateBuilder(); - - if (compilation.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemReflectionAssembly, out var assemblyType)) - { - // properties - AddIfNotNull(propertiesBuilder, TryGetSingleSymbol(assemblyType.GetMembers("Location"))); - - // methods - methodsBuilder.AddRange(assemblyType.GetMembers("GetFile").OfType()); - methodsBuilder.AddRange(assemblyType.GetMembers("GetFiles").OfType()); - } - - if (compilation.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemReflectionAssemblyName, out var assemblyNameType)) - { - AddIfNotNull(propertiesBuilder, TryGetSingleSymbol(assemblyNameType.GetMembers("CodeBase"))); - AddIfNotNull(propertiesBuilder, TryGetSingleSymbol(assemblyNameType.GetMembers("EscapedCodeBase"))); - } - - var properties = propertiesBuilder.ToImmutable(); - var methods = methodsBuilder.ToImmutable(); - - context.RegisterOperationAction(operationContext => - { - var access = (IPropertyReferenceOperation)operationContext.Operation; - var property = access.Property; - if (!Contains(properties, property, SymbolEqualityComparer.Default)) - { - return; - } - - operationContext.ReportDiagnostic(access.CreateDiagnostic(LocationRule, property)); - }, OperationKind.PropertyReference); - - context.RegisterOperationAction(operationContext => - { - var invocation = (IInvocationOperation)operationContext.Operation; - var targetMethod = invocation.TargetMethod; - if (!Contains(methods, targetMethod, SymbolEqualityComparer.Default)) - { - return; - } - - operationContext.ReportDiagnostic(invocation.CreateDiagnostic(GetFilesRule, targetMethod)); - }, OperationKind.Invocation); - - return; - - static bool Contains(ImmutableArray list, T elem, TComp comparer) - where TComp : IEqualityComparer - { - foreach (var e in list) - { - if (comparer.Equals(e, elem)) - { - return true; - } - } - return false; - } - - static TSymbol? TryGetSingleSymbol(ImmutableArray members) where TSymbol : class, ISymbol - { - TSymbol? candidate = null; - foreach (var m in members) - { - if (m is TSymbol tsym) - { - if (candidate is null) - { - candidate = tsym; - } - else - { - return null; - } - } - } - return candidate; - } - - static void AddIfNotNull(ImmutableArray.Builder properties, TSymbol? p) where TSymbol : class, ISymbol - { - if (p is not null) - { - properties.Add(p); - } - } - }); - } - } -} diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.cs.xlf b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.cs.xlf index be063a9c1a..714e9e49b4 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.cs.xlf +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.cs.xlf @@ -47,21 +47,6 @@ Literály řetězců atributů by se měly správně parsovat - - '{0}' will throw for assemblies embedded in a single-file app - Pro sestavení vložená do aplikace s jedním souborem {0} vyvolá výjimku. - - - - '{0}' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. - Pro sestavení vložená do aplikace s jedním souborem {0} vždy vrátí prázdný řetězec. Pokud se cesta k adresáři aplikace vyžaduje, zvažte možnost zavolat System.AppContext.BaseDirectory. - - - - Avoid using accessing Assembly file path when publishing as a single-file - Při publikování jednoho souboru nepoužívat přístup k cestě souboru sestavení - - Marshalling of 'StringBuilder' always creates a native buffer copy, resulting in multiple allocations for one marshalling operation. Zařazením parametru StringBuilder se vždy vytvoří kopie nativní vyrovnávací paměti, která bude mít za následek vícenásobné přidělení pro jednu operaci zařazování. diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.de.xlf b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.de.xlf index 52ca3a058f..6e0f153a85 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.de.xlf +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.de.xlf @@ -47,21 +47,6 @@ Attributzeichenfolgenliterale müssen richtig analysiert werden - - '{0}' will throw for assemblies embedded in a single-file app - "{0}" wird für Assemblys ausgelöst, die in einer Einzeldatei-App eingebettet sind. - - - - '{0}' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. - "{0}" gibt immer eine leere Zeichenfolge für Assemblys zurück, die in einer Einzeldatei-App eingebettet sind. Wenn der Pfad zum App-Verzeichnis erforderlich ist, sollten Sie "System.AppContext.BaseDirectory" aufrufen. - - - - Avoid using accessing Assembly file path when publishing as a single-file - Zugriff auf den Assemblydateipfad beim Veröffentlichen als Einzeldatei vermeiden - - Marshalling of 'StringBuilder' always creates a native buffer copy, resulting in multiple allocations for one marshalling operation. Beim Marshalling von "StringBuilder" wird immer eine native Pufferkopie erstellt, sodass mehrere Zuordnungen für einen Marshallingvorgang vorhanden sind. diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.es.xlf b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.es.xlf index 18668ee783..8c66938819 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.es.xlf +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.es.xlf @@ -47,21 +47,6 @@ Los literales de cadena de atributo se deben analizar correctamente - - '{0}' will throw for assemblies embedded in a single-file app - Se iniciará "{0}" para los ensamblados insertados en una aplicación de un solo archivo. - - - - '{0}' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. - "{0}" devuelve siempre una cadena vacía para los ensamblados insertados en una aplicación de un solo archivo. Si se necesita la ruta de acceso al directorio de la aplicación, considere la posibilidad de llamar a "System.AppContext.BaseDirectory". - - - - Avoid using accessing Assembly file path when publishing as a single-file - Evite usar la ruta de acceso al archivo de ensamblado al publicar como único archivo - - Marshalling of 'StringBuilder' always creates a native buffer copy, resulting in multiple allocations for one marshalling operation. Al serializar "StringBuilder" siempre se crea una copia del búfer nativo, lo que da lugar a varias asignaciones para una operación de serialización. diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.fr.xlf b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.fr.xlf index d90f9ec3af..71a0c15261 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.fr.xlf +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.fr.xlf @@ -47,21 +47,6 @@ Les littéraux de chaîne d'attribut doivent être analysés correctement - - '{0}' will throw for assemblies embedded in a single-file app - '{0}' lèvera une exception pour les assemblys incorporés dans une application monofichier - - - - '{0}' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. - '{0}' retourne toujours une chaîne vide pour les assemblys incorporés dans une application monofichier. Si le chemin du répertoire d'application est nécessaire, appelez 'System.AppContext.BaseDirectory'. - - - - Avoid using accessing Assembly file path when publishing as a single-file - Évitez d'utiliser l'accès du chemin du fichier d'assembly lors de la publication en tant qu'application monofichier - - Marshalling of 'StringBuilder' always creates a native buffer copy, resulting in multiple allocations for one marshalling operation. Le marshaling de 'StringBuilder' crée toujours une copie de la mémoire tampon native, ce qui entraîne plusieurs allocations pour une seule opération de marshaling. diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.it.xlf b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.it.xlf index 09c16a9f6e..61adbbacb6 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.it.xlf +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.it.xlf @@ -47,21 +47,6 @@ I valori letterali stringa dell'attributo devono essere analizzati correttamente - - '{0}' will throw for assemblies embedded in a single-file app - '{0}' verrà generato per gli assembly incorporati in un'app a file singolo - - - - '{0}' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. - '{0}' restituisce sempre una stringa vuota per gli assembly incorporati in un'app a file singolo. Se il percorso della directory app è necessario, provare a chiamare 'System.AppContext.BaseDirectory'. - - - - Avoid using accessing Assembly file path when publishing as a single-file - Evitare l'accesso al percorso del file di assembly quando si esegue la pubblicazione come file singolo - - Marshalling of 'StringBuilder' always creates a native buffer copy, resulting in multiple allocations for one marshalling operation. Il marshalling di 'StringBuilder' crea sempre una copia del buffer nativo, di conseguenza vengono generate più allocazioni per una singola operazione di marshalling. diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.ja.xlf b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.ja.xlf index 6937c685a2..4faf2ebe51 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.ja.xlf +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.ja.xlf @@ -47,21 +47,6 @@ 属性文字列リテラルは、正しく解析する必要があります - - '{0}' will throw for assemblies embedded in a single-file app - '{0}' では、単一ファイル アプリに組み込まれているアセンブリのためにスローします - - - - '{0}' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. - '{0}' は、単一ファイル アプリに埋め込まれているアセンブリに対して、常に空の文字列を返します。アプリ ディレクトリへのパスが必要な場合は、'System.AppContext.BaseDirectory' を呼び出すことを検討してください。 - - - - Avoid using accessing Assembly file path when publishing as a single-file - 単一ファイルとして発行するときにアセンブリ ファイル パスへのアクセスを使用しない - - Marshalling of 'StringBuilder' always creates a native buffer copy, resulting in multiple allocations for one marshalling operation. 'StringBuilder' をマーシャリングすると、ネイティブ バッファーのコピーが常に作成され、1 回のマーシャリング操作に対して複数の割り当てが発生します。 diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.ko.xlf b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.ko.xlf index d329428df3..42e160e005 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.ko.xlf +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.ko.xlf @@ -47,21 +47,6 @@ 특성 문자열 리터럴이 올바르게 구문 분석되어야 합니다. - - '{0}' will throw for assemblies embedded in a single-file app - '{0}'이(가) 단일 파일 앱에 포함된 어셈블리에 대해 throw합니다. - - - - '{0}' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. - '{0}'은(는) 단일 파일 앱에 포함된 어셈블리에 대해 항상 빈 문자열을 반환합니다. 앱 디렉터리 경로가 필요한 경우 'System.AppContext.BaseDirectory'를 호출하는 것이 좋습니다. - - - - Avoid using accessing Assembly file path when publishing as a single-file - 단일 파일로 게시할 때 어셈블리 파일 경로 액세스를 사용하지 마세요. - - Marshalling of 'StringBuilder' always creates a native buffer copy, resulting in multiple allocations for one marshalling operation. 'StringBuilder'를 마샬링하는 경우 항상 네이티브 버퍼 복사본이 만들어지므로 하나의 마샬링 작업에 대해 할당이 여러 번 이루어집니다. diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.pl.xlf b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.pl.xlf index 0131fbd52a..dd9ad6db14 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.pl.xlf +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.pl.xlf @@ -47,21 +47,6 @@ Analiza literałów ciągu atrybutu powinna kończyć się powodzeniem - - '{0}' will throw for assemblies embedded in a single-file app - Element „{0}” będzie zgłaszać dla zestawów osadzonych w aplikacji jednoplikowej - - - - '{0}' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. - Element „{0}” zawsze zwraca pusty ciąg dla zestawów osadzonych w aplikacji jednoplikowej. Jeśli potrzebna jest ścieżka do katalogu aplikacji, rozważ wywołanie elementu „System.AppContext.BaseDirectory”. - - - - Avoid using accessing Assembly file path when publishing as a single-file - Unikaj uzyskiwania dostępu do ścieżki pliku zestawu podczas publikowania w postaci jednoplikowej - - Marshalling of 'StringBuilder' always creates a native buffer copy, resulting in multiple allocations for one marshalling operation. Marshalling elementu „StringBuilder” zawsze tworzy natywną kopię buforu, co powoduje powstanie wielu alokacji dla jednej operacji marshallingu. diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.pt-BR.xlf b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.pt-BR.xlf index 9e63b6fc06..f73645c162 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.pt-BR.xlf +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.pt-BR.xlf @@ -47,21 +47,6 @@ Literais de cadeias de caracteres de atributos devem ser analisados corretamente - - '{0}' will throw for assemblies embedded in a single-file app - '{0}' será gerado para assemblies inseridos em um aplicativo de arquivo único - - - - '{0}' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. - '{0}' sempre retorna uma cadeia de caracteres vazia para assemblies inseridos em um aplicativo de arquivo único. Se o caminho para o diretório do aplicativo for necessário, considere chamar 'System.AppContext.BaseDirectory'. - - - - Avoid using accessing Assembly file path when publishing as a single-file - Evite usar o acesso ao Caminho do arquivo do assembly na publicação como um arquivo único - - Marshalling of 'StringBuilder' always creates a native buffer copy, resulting in multiple allocations for one marshalling operation. O marshaling de 'StringBuilder' sempre cria uma cópia de buffer nativo, resultando em várias alocações para uma operação de marshalling. diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.ru.xlf b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.ru.xlf index 6dbafe9c23..3b853eaf7e 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.ru.xlf +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.ru.xlf @@ -47,21 +47,6 @@ Синтаксический анализ строковых литералов атрибута должен осуществляться правильно - - '{0}' will throw for assemblies embedded in a single-file app - Для сборок, внедренных в приложение с одним файлом, будет генерироваться "{0}". - - - - '{0}' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. - "{0}" всегда возвращает пустую строку для сборок, внедренных в приложение с одним файлом. Если требуется путь к каталогу приложения, попробуйте вызвать "System.AppContext.BaseDirectory". - - - - Avoid using accessing Assembly file path when publishing as a single-file - Не используйте путь к файлу сборки при публикации в виде одного файла - - Marshalling of 'StringBuilder' always creates a native buffer copy, resulting in multiple allocations for one marshalling operation. При маршалировании "StringBuilder" всегда создается собственная копия буфера, что приводит к множественным выделениям для одной операции маршалирования. diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.tr.xlf b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.tr.xlf index c4dd09e3bd..e85849b540 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.tr.xlf +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.tr.xlf @@ -47,21 +47,6 @@ Öznitelik dizesinin sabit değerleri doğru ayrıştırılmalıdır - - '{0}' will throw for assemblies embedded in a single-file app - '{0}', tek dosyalı bir uygulamaya eklenen bütünleştirilmiş kodlar için özel durum oluşturur - - - - '{0}' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. - '{0}', tek dosyalı bir uygulamaya eklenen bütünleştirilmiş kodlar için her zaman boş bir dize döndürür. Uygulama dizininin yolu gerekiyorsa 'System.AppContext.BaseDirectory' özelliğini çağırmayı düşünün. - - - - Avoid using accessing Assembly file path when publishing as a single-file - Tek dosyalı olarak yayımlarken bütünleştirilmiş kod dosyası yoluna erişimi kullanmaktan kaçının - - Marshalling of 'StringBuilder' always creates a native buffer copy, resulting in multiple allocations for one marshalling operation. 'StringBuilder' öğesinin hazırlanması her zaman, bir hazırlama işlemi için birden çok ayırmaya neden olan yerel arabellek kopyası oluşturur. diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.zh-Hans.xlf b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.zh-Hans.xlf index 1a161cab86..2548f9f6f3 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.zh-Hans.xlf +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.zh-Hans.xlf @@ -47,21 +47,6 @@ 特性字符串文本应正确分析 - - '{0}' will throw for assemblies embedded in a single-file app - 对于嵌入在单文件应用中的程序集,将引发“{0}” - - - - '{0}' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. - 对于嵌入在单文件应用中的程序集,“{0}”始终返回空字符串。如果需要应用目录的路径,请考虑调用 "System.AppContext.BaseDirectory"。 - - - - Avoid using accessing Assembly file path when publishing as a single-file - 在以单文件形式发布时,避免使用访问程序集文件路径 - - Marshalling of 'StringBuilder' always creates a native buffer copy, resulting in multiple allocations for one marshalling operation. "StringBuilder" 的封送处理总是会创建一个本机缓冲区副本,这导致一个封送处理操作出现多次分配。 diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.zh-Hant.xlf b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.zh-Hant.xlf index 7bd5957db2..1aebe6df07 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.zh-Hant.xlf +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.zh-Hant.xlf @@ -47,21 +47,6 @@ 屬性字串常值應正確剖析 - - '{0}' will throw for assemblies embedded in a single-file app - '{0}' 將會為內嵌在單一檔案應用程式中的組件進行擲回 - - - - '{0}' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. - '{0}' 一律會為內嵌在單一檔案應用程式中的組件傳回空字串。若需要應用程式目錄的路徑,請考慮呼叫 'System.AppContext.BaseDirectory'。 - - - - Avoid using accessing Assembly file path when publishing as a single-file - 以單一檔案形式發佈時,避免使用正在存取的組件檔案路徑 - - Marshalling of 'StringBuilder' always creates a native buffer copy, resulting in multiple allocations for one marshalling operation. 封送處理 'StringBuilder' 一律都會建立原生緩衝區複本,因而導致單一封送處理作業出現多重配置。 diff --git a/src/NetAnalyzers/Microsoft.CodeAnalysis.NetAnalyzers.md b/src/NetAnalyzers/Microsoft.CodeAnalysis.NetAnalyzers.md index 1dff4237dc..7fe18e9c6a 100644 --- a/src/NetAnalyzers/Microsoft.CodeAnalysis.NetAnalyzers.md +++ b/src/NetAnalyzers/Microsoft.CodeAnalysis.NetAnalyzers.md @@ -2927,27 +2927,3 @@ Hard-coded certificates in source code are vulnerable to being exploited. |Severity|Warning| |CodeFix|False| --- - -## [IL3000](https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/il3000): Avoid using accessing Assembly file path when publishing as a single-file - -'{0}' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. - -|Item|Value| -|-|-| -|Category|Publish| -|Enabled|True| -|Severity|Warning| -|CodeFix|False| ---- - -## [IL3001](https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/il3001): Avoid using accessing Assembly file path when publishing as a single-file - -'{0}' will throw for assemblies embedded in a single-file app - -|Item|Value| -|-|-| -|Category|Publish| -|Enabled|True| -|Severity|Warning| -|CodeFix|False| ---- diff --git a/src/NetAnalyzers/Microsoft.CodeAnalysis.NetAnalyzers.sarif b/src/NetAnalyzers/Microsoft.CodeAnalysis.NetAnalyzers.sarif index 3f418be3a1..9646ae0112 100644 --- a/src/NetAnalyzers/Microsoft.CodeAnalysis.NetAnalyzers.sarif +++ b/src/NetAnalyzers/Microsoft.CodeAnalysis.NetAnalyzers.sarif @@ -5014,46 +5014,6 @@ "EnabledRuleInAggressiveMode" ] } - }, - "IL3000": { - "id": "IL3000", - "shortDescription": "Avoid using accessing Assembly file path when publishing as a single-file", - "fullDescription": "'{0}' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'.", - "defaultLevel": "warning", - "helpUri": "https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/il3000", - "properties": { - "category": "Publish", - "isEnabledByDefault": true, - "typeName": "AvoidAssemblyLocationInSingleFile", - "languages": [ - "C#", - "Visual Basic" - ], - "tags": [ - "Telemetry", - "EnabledRuleInAggressiveMode" - ] - } - }, - "IL3001": { - "id": "IL3001", - "shortDescription": "Avoid using accessing Assembly file path when publishing as a single-file", - "fullDescription": "'{0}' will throw for assemblies embedded in a single-file app", - "defaultLevel": "warning", - "helpUri": "https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/il3001", - "properties": { - "category": "Publish", - "isEnabledByDefault": true, - "typeName": "AvoidAssemblyLocationInSingleFile", - "languages": [ - "C#", - "Visual Basic" - ], - "tags": [ - "Telemetry", - "EnabledRuleInAggressiveMode" - ] - } } } }, diff --git a/src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Publish/AvoidAssemblyLocationInSingleFileTests.cs b/src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Publish/AvoidAssemblyLocationInSingleFileTests.cs deleted file mode 100644 index 780cb653cc..0000000000 --- a/src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Publish/AvoidAssemblyLocationInSingleFileTests.cs +++ /dev/null @@ -1,167 +0,0 @@ -// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Threading.Tasks; -using Microsoft.CodeAnalysis.Testing; -using Xunit; -using VerifyCS = Test.Utilities.CSharpCodeFixVerifier< - Microsoft.NetCore.Analyzers.Publish.AvoidAssemblyLocationInSingleFile, - Microsoft.CodeAnalysis.Testing.EmptyCodeFixProvider>; -using static Analyzer.Utilities.MSBuildPropertyOptionNames; -using static Microsoft.NetCore.Analyzers.Publish.AvoidAssemblyLocationInSingleFile; - -namespace Microsoft.NetCore.Analyzers.Publish.UnitTests -{ - public class AvoidAssemblyLocationInSingleFileTests - { - [Theory] - [CombinatorialData] - public Task GetExecutingAssemblyLocation( - [CombinatorialValues(true, false, null)] bool? publish, - [CombinatorialValues(true, false, null)] bool? includeContent) - { - const string source = @" -using System.Reflection; -class C -{ - public string M() => Assembly.GetExecutingAssembly().Location; -}"; - string analyzerConfig = ""; - if (publish is not null) - { - analyzerConfig += $"build_property.{PublishSingleFile} = {publish}" + Environment.NewLine; - } - if (includeContent is not null) - { - analyzerConfig += $"build_property.{IncludeAllContentForSelfExtract} = {includeContent}"; - } - - var test = new VerifyCS.Test - { - TestCode = source, - AnalyzerConfigDocument = analyzerConfig - }; - - DiagnosticResult[] diagnostics; - if (publish is true && includeContent is not true) - { - diagnostics = new DiagnosticResult[] { - // /0/Test0.cs(5,26): warning IL3000: 'System.Reflection.Assembly.Location' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. - VerifyCS.Diagnostic(AvoidAssemblyLocationInSingleFile.IL3000).WithSpan(5, 26, 5, 66).WithArguments("System.Reflection.Assembly.Location"), - }; - } - else - { - diagnostics = Array.Empty(); - } - - test.ExpectedDiagnostics.AddRange(diagnostics); - return test.RunAsync(); - } - - [Fact] - public Task AssemblyProperties() - { - var src = @" -using System.Reflection; -class C -{ - public void M() - { - var a = Assembly.GetExecutingAssembly(); - _ = a.Location; - // below will be obsolete in 5.0 - _ = a.CodeBase; - _ = a.EscapedCodeBase; - } -}"; - return VerifyDiagnosticsAsync(src, - // /0/Test0.cs(8,13): warning IL3000: 'System.Reflection.Assembly.Location' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. - VerifyCS.Diagnostic(AvoidAssemblyLocationInSingleFile.IL3000).WithSpan(8, 13, 8, 23).WithArguments("System.Reflection.Assembly.Location") - ); - } - - [Fact] - public Task AssemblyMethods() - { - var src = @" -using System.Reflection; -class C -{ - public void M() - { - var a = Assembly.GetExecutingAssembly(); - _ = a.GetFile(""/some/file/path""); - _ = a.GetFiles(); - } -}"; - return VerifyDiagnosticsAsync(src, - // /0/Test0.cs(8,13): warning IL3001: Assemblies embedded in a single-file app cannot have additional files in the manifest. - VerifyCS.Diagnostic(AvoidAssemblyLocationInSingleFile.IL3001).WithSpan(8, 13, 8, 41).WithArguments("System.Reflection.Assembly.GetFile(string)"), - // /0/Test0.cs(9,13): warning IL3001: Assemblies embedded in a single-file app cannot have additional files in the manifest. - VerifyCS.Diagnostic(AvoidAssemblyLocationInSingleFile.IL3001).WithSpan(9, 13, 9, 25).WithArguments("System.Reflection.Assembly.GetFiles()") - ); - } - - [Fact] - public Task AssemblyNameAttributes() - { - var src = @" -using System.Reflection; -class C -{ - public void M() - { - var a = Assembly.GetExecutingAssembly().GetName(); - _ = a.CodeBase; - _ = a.EscapedCodeBase; - } -}"; - return VerifyDiagnosticsAsync(src, - // /0/Test0.cs(8,13): warning IL3000: 'System.Reflection.AssemblyName.CodeBase' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. - VerifyCS.Diagnostic(AvoidAssemblyLocationInSingleFile.IL3000).WithSpan(8, 13, 8, 23).WithArguments("System.Reflection.AssemblyName.CodeBase"), - // /0/Test0.cs(9,13): warning IL3000: 'System.Reflection.AssemblyName.EscapedCodeBase' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. - VerifyCS.Diagnostic(AvoidAssemblyLocationInSingleFile.IL3000).WithSpan(9, 13, 9, 30).WithArguments("System.Reflection.AssemblyName.EscapedCodeBase") - ); - } - - [Fact] - public Task FalsePositive() - { - // This is an OK use of Location and GetFile since these assemblies were loaded from - // a file, but the analyzer is conservative - var src = @" -using System.Reflection; -class C -{ - public void M() - { - var a = Assembly.LoadFrom(""/some/path/not/in/bundle""); - _ = a.Location; - _ = a.GetFiles(); - } -}"; - return VerifyDiagnosticsAsync(src, - // /0/Test0.cs(8,13): warning IL3000: 'System.Reflection.Assembly.Location' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. - VerifyCS.Diagnostic(AvoidAssemblyLocationInSingleFile.IL3000).WithSpan(8, 13, 8, 23).WithArguments("System.Reflection.Assembly.Location"), - // /0/Test0.cs(9,13): warning IL3001: Assemblies embedded in a single-file app cannot have additional files in the manifest. - VerifyCS.Diagnostic(AvoidAssemblyLocationInSingleFile.IL3001).WithSpan(9, 13, 9, 25).WithArguments("System.Reflection.Assembly.GetFiles()") - ); - } - - private Task VerifyDiagnosticsAsync(string source, params DiagnosticResult[] expected) - { - const string singleFilePublishConfig = @" -build_property." + PublishSingleFile + " = true"; - - var test = new VerifyCS.Test - { - TestCode = source, - AnalyzerConfigDocument = singleFilePublishConfig - }; - - test.ExpectedDiagnostics.AddRange(expected); - return test.RunAsync(); - } - } -} \ No newline at end of file diff --git a/src/Utilities/Compiler/DiagnosticCategory.cs b/src/Utilities/Compiler/DiagnosticCategory.cs index 69def675b9..3b32ebccb3 100644 --- a/src/Utilities/Compiler/DiagnosticCategory.cs +++ b/src/Utilities/Compiler/DiagnosticCategory.cs @@ -16,7 +16,6 @@ internal static class DiagnosticCategory public const string Library = nameof(Library); public const string Documentation = nameof(Documentation); public const string Maintainability = nameof(Maintainability); - public const string Publish = nameof(Publish); public const string RoslynDiagnosticsDesign = nameof(RoslynDiagnosticsDesign); public const string RoslynDiagnosticsMaintainability = nameof(RoslynDiagnosticsMaintainability); diff --git a/src/Utilities/Compiler/DiagnosticCategoryAndIdRanges.txt b/src/Utilities/Compiler/DiagnosticCategoryAndIdRanges.txt index 793ed1c657..94d4940230 100644 --- a/src/Utilities/Compiler/DiagnosticCategoryAndIdRanges.txt +++ b/src/Utilities/Compiler/DiagnosticCategoryAndIdRanges.txt @@ -35,8 +35,4 @@ RoslynDiagnosticsDesign: RS0000-RS0999 RoslynDiagnosticsMaintainability: RS0000-RS0999 RoslynDiagnosticsPerformance: RS0000-RS0999 RoslynDiagnosticsReliability: RS0000-RS0999 -RoslynDiagnosticsUsage: RS0000-RS0999 - -# dotnet publish rules -# These are warnings for single-file publish or the IL trimmer -Publish: IL0000-IL9999 \ No newline at end of file +RoslynDiagnosticsUsage: RS0000-RS0999 \ No newline at end of file diff --git a/src/Utilities/Compiler/Options/MSBuildPropertyOptionNames.cs b/src/Utilities/Compiler/Options/MSBuildPropertyOptionNames.cs index 891cd4de94..5e4940793b 100644 --- a/src/Utilities/Compiler/Options/MSBuildPropertyOptionNames.cs +++ b/src/Utilities/Compiler/Options/MSBuildPropertyOptionNames.cs @@ -17,8 +17,6 @@ internal static class MSBuildPropertyOptionNames public const string TargetPlatformMinVersion = nameof(TargetPlatformMinVersion); public const string UsingMicrosoftNETSdkWeb = nameof(UsingMicrosoftNETSdkWeb); public const string ProjectTypeGuids = nameof(ProjectTypeGuids); - public const string PublishSingleFile = nameof(PublishSingleFile); - public const string IncludeAllContentForSelfExtract = nameof(IncludeAllContentForSelfExtract); } internal static class MSBuildPropertyOptionNamesHelpers From 89f58a2361a8252f93b2bbbbc8f0e033e311b136 Mon Sep 17 00:00:00 2001 From: Manish Vasani Date: Mon, 11 Jan 2021 09:46:14 -0800 Subject: [PATCH 6/6] Update src/NetAnalyzers/Core/AnalyzerReleases.Unshipped.md --- src/NetAnalyzers/Core/AnalyzerReleases.Unshipped.md | 1 + 1 file changed, 1 insertion(+) diff --git a/src/NetAnalyzers/Core/AnalyzerReleases.Unshipped.md b/src/NetAnalyzers/Core/AnalyzerReleases.Unshipped.md index 332cd78356..64372afc0b 100644 --- a/src/NetAnalyzers/Core/AnalyzerReleases.Unshipped.md +++ b/src/NetAnalyzers/Core/AnalyzerReleases.Unshipped.md @@ -1,6 +1,7 @@ ; Please do not edit this file manually, it should only be updated through code fix application. ### Removed Rules + Rule ID | Category | Severity | Notes --------|----------|----------|------- CA1801 | Usage | Disabled | ReviewUnusedParametersAnalyzer, [Documentation](https://docs.microsoft.com/visualstudio/code-quality/ca1801)