From 7c915fdbfa71c7a853bb406c605b5b493e65c787 Mon Sep 17 00:00:00 2001 From: Buyaa Namnan Date: Thu, 21 May 2020 10:36:37 -0700 Subject: [PATCH] Add parameter check --- .../InstantiateArgumentExceptionsCorrectly.cs | 8 ++-- ...antiateArgumentExceptionsCorrectlyTests.cs | 40 +++++++++++++++++++ 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/InstantiateArgumentExceptionsCorrectly.cs b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/InstantiateArgumentExceptionsCorrectly.cs index 67491def79..928bd1ac79 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/InstantiateArgumentExceptionsCorrectly.cs +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/InstantiateArgumentExceptionsCorrectly.cs @@ -86,14 +86,14 @@ private static void AnalyzeObjectCreation( ITypeSymbol argumentExceptionType) { var creation = (IObjectCreationOperation)context.Operation; - if (!creation.Type.Inherits(argumentExceptionType) || !MatchesConfiguredVisibility(owningSymbol, context)) + if (!creation.Type.Inherits(argumentExceptionType) || !MatchesConfiguredVisibility(owningSymbol, context) || !HasParameterNameConstructor(creation.Type)) { return; } if (creation.Arguments.Length == 0) { - if (HasParameters(owningSymbol) && HasMessageOrParameterNameConstructor(creation.Type)) + if (HasParameters(owningSymbol)) { // Call the {0} constructor that contains a message and/ or paramName parameter context.ReportDiagnostic(context.Operation.Syntax.CreateDiagnostic(RuleNoArguments, creation.Type.Name)); @@ -174,7 +174,7 @@ private static bool IsParameterName(IParameterSymbol parameter) return parameter.Name == "paramName" || parameter.Name == "parameterName"; } - private static bool HasMessageOrParameterNameConstructor(ITypeSymbol type) + private static bool HasParameterNameConstructor(ITypeSymbol type) { foreach (ISymbol member in type.GetMembers()) { @@ -186,7 +186,7 @@ private static bool HasMessageOrParameterNameConstructor(ITypeSymbol type) foreach (IParameterSymbol parameter in member.GetParameters()) { if (parameter.Type.SpecialType == SpecialType.System_String - && (IsMessage(parameter) || IsParameterName(parameter))) + && IsParameterName(parameter)) { return true; } diff --git a/src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Runtime/InstantiateArgumentExceptionsCorrectlyTests.cs b/src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Runtime/InstantiateArgumentExceptionsCorrectlyTests.cs index 919263e8f3..255ac3d99d 100644 --- a/src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Runtime/InstantiateArgumentExceptionsCorrectlyTests.cs +++ b/src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Runtime/InstantiateArgumentExceptionsCorrectlyTests.cs @@ -915,6 +915,46 @@ End Sub End Class"); } + [Fact] + public async Task ArgumentExceptionType_NotHavingConstructorWithParameterName_NoArgument_DoesNotWarn() + { + await VerifyCS.VerifyAnalyzerAsync(@" + public class Class + { + public void Test(string first) + { + throw new System.Text.DecoderFallbackException (); + } + }"); + + await VerifyVB.VerifyAnalyzerAsync(@" + Public Class [MyClass] + Public Sub Test(first As String) + Throw New System.Text.DecoderFallbackException () + End Sub + End Class"); + } + + [Fact] + public async Task ArgumentExceptionType_NotHavingConstructor_WithParameterName_WithArgument_DoesNotWarn() + { + await VerifyCS.VerifyAnalyzerAsync(@" + public class Class + { + public void Test(string first) + { + throw new System.Text.DecoderFallbackException (""first""); + } + }"); + + await VerifyVB.VerifyAnalyzerAsync(@" + Public Class [MyClass] + Public Sub Test(first As String) + Throw New System.Text.DecoderFallbackException (""first"") + End Sub + End Class"); + } + [Fact, WorkItem(1824, "https://github.com/dotnet/roslyn-analyzers/issues/1824")] public async Task ArgumentNullException_LocalFunctionParameter_DoesNotWarn() {