From 2a94244482b7a116c1f4b15d83cda0f0688d5931 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9rald=20Barr=C3=A9?= Date: Mon, 22 Jan 2024 20:49:39 -0500 Subject: [PATCH] MA0011 skips Convert.ToChar/ToBoolean --- .../Rules/UseIFormatProviderAnalyzer.cs | 27 +++++++- .../Rules/UseIFormatProviderAnalyzerTests.cs | 68 +++++++++++++++++++ 2 files changed, 93 insertions(+), 2 deletions(-) diff --git a/src/Meziantou.Analyzer/Rules/UseIFormatProviderAnalyzer.cs b/src/Meziantou.Analyzer/Rules/UseIFormatProviderAnalyzer.cs index 40f93eb4f..d1490efd8 100644 --- a/src/Meziantou.Analyzer/Rules/UseIFormatProviderAnalyzer.cs +++ b/src/Meziantou.Analyzer/Rules/UseIFormatProviderAnalyzer.cs @@ -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; @@ -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 _excludedMethods = CreateExcludedMethods(compilation); + + private static HashSet CreateExcludedMethods(Compilation compilation) + { + var result = new HashSet(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 result, Compilation compilation, string id) + { + foreach (var item in DocumentationCommentId.GetSymbolsForDeclarationId(id, compilation)) + { + result.Add(item); + } + } + } public void AnalyzeInvocation(OperationAnalysisContext context) { @@ -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") { diff --git a/tests/Meziantou.Analyzer.Test/Rules/UseIFormatProviderAnalyzerTests.cs b/tests/Meziantou.Analyzer.Test/Rules/UseIFormatProviderAnalyzerTests.cs index d1031ded1..e68cc045a 100644 --- a/tests/Meziantou.Analyzer.Test/Rules/UseIFormatProviderAnalyzerTests.cs +++ b/tests/Meziantou.Analyzer.Test/Rules/UseIFormatProviderAnalyzerTests.cs @@ -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)