-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
141 additions
and
3 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,4 +10,4 @@ class Test | |
string a; // not-compliant as it hides the field | ||
} | ||
} | ||
```` | ||
```` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,4 @@ class Test | |
throw new Exception(); // non-compliant | ||
} | ||
} | ||
```` | ||
```` |
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,15 @@ | ||
# MA0087 - Parameters with [DefaultParameterValue] attributes should also be marked [Optional] | ||
|
||
````csharp | ||
using System.Runtime.InteropServices; | ||
class Test | ||
{ | ||
void A([Optional, DefaultParameterValue(10)]int a) | ||
{ | ||
} | ||
|
||
void B([DefaultParameterValue(10)]int a) // Non-compliant | ||
{ | ||
} | ||
} | ||
```` |
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
60 changes: 60 additions & 0 deletions
60
src/Meziantou.Analyzer/Rules/OptionalParametersAttributeAnalyzer.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,60 @@ | ||
using System.Collections.Immutable; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
|
||
namespace Meziantou.Analyzer.Rules | ||
{ | ||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
public sealed class OptionalParametersAttributeAnalyzer : DiagnosticAnalyzer | ||
{ | ||
private static readonly DiagnosticDescriptor s_rule = new DiagnosticDescriptor( | ||
RuleIdentifiers.ParametersWithDefaultValueShouldBeMarkedWithOptionalParameter, | ||
title: "Parameters with [DefaultParameterValue] attributes should also be marked [Optional]", | ||
messageFormat: "Parameters with [DefaultParameterValue] attributes should also be marked [Optional]", | ||
RuleCategories.Design, | ||
DiagnosticSeverity.Warning, | ||
isEnabledByDefault: true, | ||
description: "", | ||
helpLinkUri: RuleIdentifiers.GetHelpUri(RuleIdentifiers.ParametersWithDefaultValueShouldBeMarkedWithOptionalParameter)); | ||
|
||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(s_rule); | ||
|
||
public override void Initialize(AnalysisContext context) | ||
{ | ||
context.EnableConcurrentExecution(); | ||
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); | ||
|
||
context.RegisterCompilationStartAction(ctx => | ||
{ | ||
var analyzerContext = new AnalyzerContext(ctx.Compilation); | ||
if (analyzerContext.IsValid) | ||
{ | ||
ctx.RegisterSymbolAction(analyzerContext.AnalyzerParameter, SymbolKind.Parameter); | ||
} | ||
}); | ||
} | ||
|
||
private sealed class AnalyzerContext | ||
{ | ||
public AnalyzerContext(Compilation compilation) | ||
{ | ||
OptionalAttributeSymbol = compilation.GetTypeByMetadataName("System.Runtime.InteropServices.OptionalAttribute"); | ||
DefaultParameterValueAttributeSymbol = compilation.GetTypeByMetadataName("System.Runtime.InteropServices.DefaultParameterValueAttribute"); | ||
} | ||
|
||
public INamedTypeSymbol? OptionalAttributeSymbol { get; set; } | ||
public INamedTypeSymbol? DefaultParameterValueAttributeSymbol { get; set; } | ||
|
||
public bool IsValid => OptionalAttributeSymbol != null && DefaultParameterValueAttributeSymbol != null; | ||
|
||
public void AnalyzerParameter(SymbolAnalysisContext context) | ||
{ | ||
var parameter = (IParameterSymbol)context.Symbol; | ||
if (parameter.HasAttribute(DefaultParameterValueAttributeSymbol) && !parameter.HasAttribute(OptionalAttributeSymbol)) | ||
{ | ||
context.ReportDiagnostic(s_rule, parameter); | ||
} | ||
} | ||
} | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
tests/Meziantou.Analyzer.Test/Rules/OptionalParametersAttributeAnalyzerTests.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,61 @@ | ||
using System.Threading.Tasks; | ||
using Meziantou.Analyzer.Rules; | ||
using TestHelper; | ||
using Xunit; | ||
|
||
namespace Meziantou.Analyzer.Test.Rules | ||
{ | ||
public sealed class OptionalParametersAttributeAnalyzerTests | ||
{ | ||
private static ProjectBuilder CreateProjectBuilder() | ||
{ | ||
return new ProjectBuilder() | ||
.WithAnalyzer<OptionalParametersAttributeAnalyzer>(); | ||
} | ||
|
||
[Fact] | ||
public async Task MissingOptionalAttribute() | ||
{ | ||
const string SourceCode = @"using System.Runtime.InteropServices; | ||
class Test | ||
{ | ||
void A([DefaultParameterValue(10)]int [|a|]) | ||
{ | ||
} | ||
}"; | ||
await CreateProjectBuilder() | ||
.WithSourceCode(SourceCode) | ||
.ValidateAsync(); | ||
} | ||
|
||
[Fact] | ||
public async Task BothAttributes() | ||
{ | ||
const string SourceCode = @"using System.Runtime.InteropServices; | ||
class Test | ||
{ | ||
void A([Optional, DefaultParameterValue(10)]int a) | ||
{ | ||
} | ||
}"; | ||
await CreateProjectBuilder() | ||
.WithSourceCode(SourceCode) | ||
.ValidateAsync(); | ||
} | ||
|
||
[Fact] | ||
public async Task OptionalAttribute() | ||
{ | ||
const string SourceCode = @"using System.Runtime.InteropServices; | ||
class Test | ||
{ | ||
void A([Optional]int a) | ||
{ | ||
} | ||
}"; | ||
await CreateProjectBuilder() | ||
.WithSourceCode(SourceCode) | ||
.ValidateAsync(); | ||
} | ||
} | ||
} |