-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support VB for ImmutableObjectMethodAnalyzer
- Loading branch information
1 parent
f361553
commit ad3be7b
Showing
18 changed files
with
229 additions
and
155 deletions.
There are no files selected for viewing
111 changes: 0 additions & 111 deletions
111
src/Microsoft.CodeAnalysis.Analyzers/CSharp/CSharpImmutableObjectMethodAnalyzer.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
src/Microsoft.CodeAnalysis.Analyzers/Core/ImmutableObjectMethodAnalyzer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using Analyzer.Utilities; | ||
using Analyzer.Utilities.Extensions; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Microsoft.CodeAnalysis.Operations; | ||
|
||
namespace Microsoft.CodeAnalysis.Analyzers | ||
{ | ||
using static CodeAnalysisDiagnosticsResources; | ||
|
||
/// <summary> | ||
/// RS1014: <inheritdoc cref="DoNotIgnoreReturnValueOnImmutableObjectMethodInvocationTitle"/> | ||
/// </summary> | ||
[DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)] | ||
public sealed class ImmutableObjectMethodAnalyzer : DiagnosticAnalyzer | ||
{ | ||
public static readonly DiagnosticDescriptor DoNotIgnoreReturnValueDiagnosticRule = new( | ||
DiagnosticIds.DoNotIgnoreReturnValueOnImmutableObjectMethodInvocation, | ||
CreateLocalizableResourceString(nameof(DoNotIgnoreReturnValueOnImmutableObjectMethodInvocationTitle)), | ||
CreateLocalizableResourceString(nameof(DoNotIgnoreReturnValueOnImmutableObjectMethodInvocationMessage)), | ||
DiagnosticCategory.MicrosoftCodeAnalysisCorrectness, | ||
DiagnosticSeverity.Warning, | ||
isEnabledByDefault: true, | ||
description: CreateLocalizableResourceString(nameof(DoNotIgnoreReturnValueOnImmutableObjectMethodInvocationDescription)), | ||
customTags: WellKnownDiagnosticTagsExtensions.Telemetry); | ||
|
||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(DoNotIgnoreReturnValueDiagnosticRule); | ||
|
||
private const string SolutionFullName = @"Microsoft.CodeAnalysis.Solution"; | ||
private const string ProjectFullName = @"Microsoft.CodeAnalysis.Project"; | ||
private const string DocumentFullName = @"Microsoft.CodeAnalysis.Document"; | ||
private const string SyntaxNodeFullName = @"Microsoft.CodeAnalysis.SyntaxNode"; | ||
private const string CompilationFullName = @"Microsoft.CodeAnalysis.Compilation"; | ||
|
||
private static readonly ImmutableArray<string> s_immutableMethodNames = ImmutableArray.Create( | ||
"Add", | ||
"Remove", | ||
"Replace", | ||
"With"); | ||
|
||
public override void Initialize(AnalysisContext context) | ||
{ | ||
context.EnableConcurrentExecution(); | ||
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); | ||
|
||
context.RegisterCompilationStartAction(context => | ||
{ | ||
var compilation = context.Compilation; | ||
var builder = ImmutableArray.CreateBuilder<INamedTypeSymbol>(); | ||
var provider = WellKnownTypeProvider.GetOrCreate(compilation); | ||
AddIfNotNull(builder, provider.GetOrCreateTypeByMetadataName(SolutionFullName)); | ||
AddIfNotNull(builder, provider.GetOrCreateTypeByMetadataName(ProjectFullName)); | ||
AddIfNotNull(builder, provider.GetOrCreateTypeByMetadataName(DocumentFullName)); | ||
AddIfNotNull(builder, provider.GetOrCreateTypeByMetadataName(SyntaxNodeFullName)); | ||
AddIfNotNull(builder, provider.GetOrCreateTypeByMetadataName(CompilationFullName)); | ||
var immutableTypeSymbols = builder.ToImmutable(); | ||
if (immutableTypeSymbols.Length > 0) | ||
{ | ||
context.RegisterOperationAction(context => AnalyzeInvocationForIgnoredReturnValue(context, immutableTypeSymbols), OperationKind.Invocation); | ||
} | ||
}); | ||
|
||
static void AddIfNotNull(ImmutableArray<INamedTypeSymbol>.Builder builder, INamedTypeSymbol? symbol) | ||
{ | ||
if (symbol is not null) | ||
{ | ||
builder.Add(symbol); | ||
} | ||
} | ||
} | ||
|
||
public static void AnalyzeInvocationForIgnoredReturnValue(OperationAnalysisContext context, ImmutableArray<INamedTypeSymbol> immutableTypeSymbols) | ||
{ | ||
var invocation = (IInvocationOperation)context.Operation; | ||
if (invocation.Parent is not IExpressionStatementOperation) | ||
{ | ||
return; | ||
} | ||
|
||
// If the method doesn't start with something like "With" or "Replace", quit | ||
string methodName = invocation.TargetMethod.Name; | ||
if (!s_immutableMethodNames.Any(n => methodName.StartsWith(n, StringComparison.Ordinal))) | ||
{ | ||
return; | ||
} | ||
|
||
INamedTypeSymbol? type = null; | ||
if (invocation.Instance is not null) | ||
{ | ||
type = invocation.Instance.Type as INamedTypeSymbol; | ||
} | ||
else if (invocation.TargetMethod.IsExtensionMethod && invocation.TargetMethod.Parameters is [{ Type: var parameterType }, ..]) | ||
{ | ||
type = parameterType as INamedTypeSymbol; | ||
} | ||
|
||
// If we're not in one of the known immutable types, quit | ||
if (type is not null && type.GetBaseTypesAndThis().Any(immutableTypeSymbols.Contains)) | ||
{ | ||
context.ReportDiagnostic(invocation.CreateDiagnostic(DoNotIgnoreReturnValueDiagnosticRule, type.Name, methodName)); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.