Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for constructor with parameterName parameter #3665

Merged
merged 1 commit into from
May 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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())
{
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One of the exceptions derived from ArgumentException but not have a constructor with paramName argument

}
}");

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()
{
Expand Down