Skip to content

Commit

Permalink
Merge pull request #5102 from NewellClark/#5072-CA1305-ignore-nullabl…
Browse files Browse the repository at this point in the history
…e-invariant-types

#5072 ca1305 ignore nullable invariant types
  • Loading branch information
mavasani authored May 20, 2021
2 parents 6f09d65 + 3a7a655 commit d0caaad
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ private static bool IsValidToStringCall(IInvocationOperation invocationOperation
return false;
}

if (invariantToStringTypes.Contains(targetMethod.ContainingType))
if (invariantToStringTypes.Contains(UnwrapNullableValueTypes(targetMethod.ContainingType)))
{
return true;
}
Expand All @@ -289,6 +289,15 @@ private static bool IsValidToStringCall(IInvocationOperation invocationOperation
}

return false;

// Local functions

static INamedTypeSymbol UnwrapNullableValueTypes(INamedTypeSymbol typeSymbol)
{
if (typeSymbol.IsNullableValueType() && typeSymbol.TypeArguments[0] is INamedTypeSymbol nullableTypeArgument)
return nullableTypeArgument;
return typeSymbol;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,36 @@ End Class
");
}

[Fact]
public async Task CA1305_NullableInvariantTypes_NoDiagnostic()
{
await VerifyCS.VerifyAnalyzerAsync(@"
using System;
public class SomeClass
{
private char? _char;
private bool? _bool;
private Guid? _guid;
public string SomeMethod()
{
return _char.ToString() + _bool.ToString() + _guid.ToString();
}
}");

await VerifyVB.VerifyAnalyzerAsync(@"
Imports System
Public Class SomeClass
Private _char As Char?
Private _bool As Boolean?
Private _guid As Guid?
Public Function SomeMethod() As String
Return _char.ToString() & _bool.ToString() & _guid.ToString()
End Function
End Class");
}

[Theory, WorkItem(3507, "https://github.com/dotnet/roslyn-analyzers/issues/3507")]
[InlineData("DateTime")]
[InlineData("DateTimeOffset")]
Expand Down

0 comments on commit d0caaad

Please sign in to comment.