Skip to content

Commit

Permalink
MA0011 skips Convert.ToChar/ToBoolean
Browse files Browse the repository at this point in the history
  • Loading branch information
meziantou committed Jan 23, 2024
1 parent 1ddf530 commit 2a94244
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/Meziantou.Analyzer/Rules/UseIFormatProviderAnalyzer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Immutable;
using System.Collections.Generic;
using System.Collections.Immutable;
using Meziantou.Analyzer.Configurations;
using Meziantou.Analyzer.Internals;
using Microsoft.CodeAnalysis;
Expand Down Expand Up @@ -37,6 +38,25 @@ private sealed class AnalyzerContext(Compilation compilation)
{
private readonly CultureSensitiveFormattingContext _cultureSensitiveContext = new(compilation);
private readonly OverloadFinder _overloadFinder = new(compilation);
private readonly HashSet<ISymbol> _excludedMethods = CreateExcludedMethods(compilation);

private static HashSet<ISymbol> CreateExcludedMethods(Compilation compilation)
{
var result = new HashSet<ISymbol>(SymbolEqualityComparer.Default);
AddDocumentationId(result, compilation, "M:System.Convert.ToChar(System.String)");
AddDocumentationId(result, compilation, "M:System.Convert.ToChar(System.Object)");
AddDocumentationId(result, compilation, "M:System.Convert.ToBoolean(System.String)");
AddDocumentationId(result, compilation, "M:System.Convert.ToBoolean(System.Object)");
return result;

static void AddDocumentationId(HashSet<ISymbol> result, Compilation compilation, string id)
{
foreach (var item in DocumentationCommentId.GetSymbolsForDeclarationId(id, compilation))
{
result.Add(item);
}
}
}

public void AnalyzeInvocation(OperationAnalysisContext context)
{
Expand Down Expand Up @@ -95,8 +115,11 @@ public void AnalyzeInvocation(OperationAnalysisContext context)
}
}

private static bool IsExcludedMethod(OperationAnalysisContext context, IOperation operation)
private bool IsExcludedMethod(OperationAnalysisContext context, IInvocationOperation operation)
{
if (_excludedMethods.Contains(operation.TargetMethod))
return true;

// ToString show culture-sensitive data by default
if (operation?.GetContainingMethod(context.CancellationToken)?.Name == "ToString")
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,74 @@ public void Test()
_ = [||]string.Format("", 0, 0, 0, 0, 0, 0, -1, 0 ,0 ,0, 0);
}
}
""";
await CreateProjectBuilder()
.WithSourceCode(sourceCode)
.ValidateAsync();
}

[Fact]
public async Task Convert_ToChar_Object()
{
var sourceCode = $$"""
class TypeName
{
public void Test()
{
_ = System.Convert.ToChar((object)null);
}
}
""";
await CreateProjectBuilder()
.WithSourceCode(sourceCode)
.ValidateAsync();
}

[Fact]
public async Task Convert_ToChar_String()
{
var sourceCode = $$"""
class TypeName
{
public void Test()
{
_ = System.Convert.ToChar("");
}
}
""";
await CreateProjectBuilder()
.WithSourceCode(sourceCode)
.ValidateAsync();
}

[Fact]
public async Task Convert_ToBoolean_Object()
{
var sourceCode = $$"""
class TypeName
{
public void Test()
{
_ = System.Convert.ToBoolean((object)null);
}
}
""";
await CreateProjectBuilder()
.WithSourceCode(sourceCode)
.ValidateAsync();
}

[Fact]
public async Task Convert_ToBoolean_String()
{
var sourceCode = $$"""
class TypeName
{
public void Test()
{
_ = System.Convert.ToBoolean("");
}
}
""";
await CreateProjectBuilder()
.WithSourceCode(sourceCode)
Expand Down

0 comments on commit 2a94244

Please sign in to comment.