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

Do not suggest passing IFormatProvider to certain Convert methods #7244

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
Expand Down Expand Up @@ -147,8 +148,6 @@ protected override void InitializeWorker(CompilationStartAnalysisContext context
GetParameterInfo(stringType),
GetParameterInfo(objectType, isArray: true, arrayRank: 1, isParams: true));

var currentCultureProperty = cultureInfoType.GetMembers("CurrentCulture").OfType<IPropertySymbol>().FirstOrDefault();
var invariantCultureProperty = cultureInfoType.GetMembers("InvariantCulture").OfType<IPropertySymbol>().FirstOrDefault();
var currentUICultureProperty = cultureInfoType.GetMembers("CurrentUICulture").OfType<IPropertySymbol>().FirstOrDefault();
var installedUICultureProperty = cultureInfoType.GetMembers("InstalledUICulture").OfType<IPropertySymbol>().FirstOrDefault();

Expand All @@ -165,6 +164,27 @@ protected override void InitializeWorker(CompilationStartAnalysisContext context

var guidParseMethods = guidType?.GetMembers("Parse") ?? ImmutableArray<ISymbol>.Empty;

var convertType = typeProvider.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemConvert);
var superfluousConvertToStringFormatProviderOverloads = convertType?.GetMembers(nameof(Convert.ToString))
.OfType<IMethodSymbol>()
.Where(m => m.IsStatic
&& m.Parameters is [{ Type.SpecialType: SpecialType.System_String or SpecialType.System_Boolean or SpecialType.System_Char }, var possibleFormatProvider]
&& possibleFormatProvider.Type.Equals(iformatProviderType, SymbolEqualityComparer.Default)) ?? [];
var superfluousConvertToToCharFormatProviderOverloads = convertType?.GetMembers(nameof(Convert.ToChar))
.OfType<IMethodSymbol>()
.Where(m => m.IsStatic
&& m.Parameters is [{ Type.SpecialType: SpecialType.System_String }, var possibleFormatProvider]
&& possibleFormatProvider.Type.Equals(iformatProviderType, SymbolEqualityComparer.Default)) ?? [];
var superfluousConvertToBooleanFormatProviderOverloads = convertType?.GetMembers(nameof(Convert.ToBoolean))
.OfType<IMethodSymbol>()
.Where(m => m.IsStatic
&& m.Parameters is [{ Type.SpecialType: SpecialType.System_String }, var possibleFormatProvider]
&& possibleFormatProvider.Type.Equals(iformatProviderType, SymbolEqualityComparer.Default)) ?? [];
ImmutableHashSet<IMethodSymbol> superfluousFormatProviderOverloads = superfluousConvertToStringFormatProviderOverloads
.Concat(superfluousConvertToToCharFormatProviderOverloads)
.Concat(superfluousConvertToBooleanFormatProviderOverloads)
.ToImmutableHashSet<IMethodSymbol>(SymbolEqualityComparer.Default);

#endregion

context.RegisterOperationAction(oaContext =>
Expand Down Expand Up @@ -224,7 +244,7 @@ protected override void InitializeWorker(CompilationStartAnalysisContext context

// Sample message for IFormatProviderAlternateRule: Because the behavior of Convert.ToInt64(string) could vary based on the current user's locale settings,
// replace this call in IFormatProviderStringTest.TestMethod() with a call to Convert.ToInt64(string, IFormatProvider).
if (correctOverload != null)
if (correctOverload != null && !superfluousFormatProviderOverloads.Contains(correctOverload))
{
oaContext.ReportDiagnostic(
invocationExpression.Syntax.CreateDiagnostic(
Expand Down