Skip to content

Commit bce8f95

Browse files
authored
Fixed: Do not report AV1553 on interface implementations and overridden members (#141)
1 parent 70424b5 commit bce8f95

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

src/CSharpGuidelinesAnalyzer/CSharpGuidelinesAnalyzer.Test/Specs/Maintainability/DoNotUseOptionalParameterWithDefaultValueNullSpecs.cs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,75 @@ await VerifyGuidelineDiagnosticAsync(source,
215215
"Optional parameter 'p' of type 'ValueTask<int>?' has default value 'null'");
216216
}
217217

218+
[Fact]
219+
internal async Task When_method_implicitly_implements_interface_method_it_must_be_skipped()
220+
{
221+
// Arrange
222+
ParsedSourceCode source = new TypeSourceCodeBuilder()
223+
.WithReferenceToExternalAssemblyFor(@"
224+
public interface I
225+
{
226+
void M(string s = null);
227+
}
228+
")
229+
.InGlobalScope(@"
230+
class C : I
231+
{
232+
public void M(string s = null) => throw null;
233+
}
234+
")
235+
.Build();
236+
237+
// Act and assert
238+
await VerifyGuidelineDiagnosticAsync(source);
239+
}
240+
241+
[Fact]
242+
internal async Task When_method_explicitly_implements_interface_method_it_must_be_skipped()
243+
{
244+
// Arrange
245+
ParsedSourceCode source = new TypeSourceCodeBuilder()
246+
.WithReferenceToExternalAssemblyFor(@"
247+
public interface I
248+
{
249+
void M(string s = null);
250+
}
251+
")
252+
.InGlobalScope(@"
253+
class C : I
254+
{
255+
void I.M(string s = null) => throw null;
256+
}
257+
")
258+
.Build();
259+
260+
// Act and assert
261+
await VerifyGuidelineDiagnosticAsync(source);
262+
}
263+
264+
[Fact]
265+
internal async Task When_method_overrides_base_method_it_must_be_skipped()
266+
{
267+
// Arrange
268+
ParsedSourceCode source = new TypeSourceCodeBuilder()
269+
.WithReferenceToExternalAssemblyFor(@"
270+
public abstract class B
271+
{
272+
public virtual void M(string s = null) => throw null;
273+
}
274+
")
275+
.InGlobalScope(@"
276+
class C : B
277+
{
278+
public override void M(string s = null) => throw null;
279+
}
280+
")
281+
.Build();
282+
283+
// Act and assert
284+
await VerifyGuidelineDiagnosticAsync(source);
285+
}
286+
218287
protected override DiagnosticAnalyzer CreateAnalyzer()
219288
{
220289
return new DoNotUseOptionalParameterWithDefaultValueNullAnalyzer();

src/CSharpGuidelinesAnalyzer/CSharpGuidelinesAnalyzer/Rules/Maintainability/DoNotUseOptionalParameterWithDefaultValueNullAnalyzer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ private static void AnalyzeParameter(SymbolAnalysisContext context, [NotNull] [I
8282
{
8383
var parameter = (IParameterSymbol)context.Symbol;
8484

85-
if (parameter.IsOptional && parameter.HasExplicitDefaultValue && parameter.ExplicitDefaultValue == null &&
85+
if (parameter.IsOptional && parameter.HasExplicitDefaultValue && parameter.ExplicitDefaultValue == null && !parameter.ContainingSymbol.IsOverride &&
86+
!parameter.ContainingSymbol.IsInterfaceImplementation() &&
8687
!HasCallerArgumentExpressionAttribute(parameter, callerArgumentExpressionAttributeType))
8788
{
8889
if (parameter.Type.IsOrImplementsIEnumerable() || IsTask(parameter.Type, taskTypes))

0 commit comments

Comments
 (0)