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

Merged
merged 3 commits into from
Aug 30, 2024

Conversation

CollinAlpert
Copy link
Contributor

Affected analyzer: SpecifyIFormatProviderAnalyzer
Affected diagnostic ID: CA1305

This PR prevents suggesting to pass an IFormatProvider to Convert.ToX methods where passing an IFormatProvider would change nothing.

Fixes #7154

@CollinAlpert CollinAlpert requested a review from a team as a code owner March 13, 2024 23:11
Copy link

codecov bot commented Mar 13, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.49%. Comparing base (68eda2d) to head (65a0697).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff            @@
##             main    #7244    +/-   ##
========================================
  Coverage   96.49%   96.49%            
========================================
  Files        1443     1443            
  Lines      345972   346376   +404     
  Branches    11383    11387     +4     
========================================
+ Hits       333856   334252   +396     
- Misses       9235     9242     +7     
- Partials     2881     2882     +1     

Comment on lines 168 to 186
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);
Copy link
Contributor

Choose a reason for hiding this comment

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

Got some NITs:

  • Better to check null before head to avoid null check each reference and creating empty array
  • For ToChar, ToBoolean we expect only one method to get, no need to create array for this
  • m.IsStatic is redundant as Convert is a static type, all methods are static anyways
    Something like:
Suggested change
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);
ImmutableHashSet<IMethodSymbol> superfluousFormatProviderOverloads = ImmutableHashSet<IMethodSymbol>.Empty;
if (convertType != null)
{
superfluousFormatProviderOverloads = convertType.GetMembers(nameof(Convert.ToString))
.OfType<IMethodSymbol>()
.Where(m => m.Parameters is [{ Type.SpecialType: SpecialType.System_String or SpecialType.System_Boolean or SpecialType.System_Char }, var possibleFormatProvider]
&& possibleFormatProvider.Type.Equals(iformatProviderType, SymbolEqualityComparer.Default)).ToImmutableHashSet();
superfluousFormatProviderOverloads = superfluousFormatProviderOverloads
.Add(convertType.GetMembers(nameof(Convert.ToChar))
.OfType<IMethodSymbol>()
.First(m => m.Parameters is [{ Type.SpecialType: SpecialType.System_String }, var possibleFormatProvider]
&& possibleFormatProvider.Type.Equals(iformatProviderType, SymbolEqualityComparer.Default)))
.Add(convertType.GetMembers(nameof(Convert.ToBoolean))
.OfType<IMethodSymbol>()
.First(m => m.Parameters is [{ Type.SpecialType: SpecialType.System_String }, var possibleFormatProvider]
&& possibleFormatProvider.Type.Equals(iformatProviderType, SymbolEqualityComparer.Default)));
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the review!

Copy link
Contributor

@buyaa-n buyaa-n left a comment

Choose a reason for hiding this comment

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

Overall LGTM, thanks @CollinAlpert

Copy link
Contributor

@buyaa-n buyaa-n left a comment

Choose a reason for hiding this comment

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

Thank you!

@buyaa-n buyaa-n merged commit 372f0d4 into dotnet:main Aug 30, 2024
11 checks passed
@CollinAlpert CollinAlpert deleted the issue-7154 branch September 2, 2024 16:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CA1305 false positives: Convert.ToString(bool), Convert.ToChar(string), Convert.ToBoolean(strng)
2 participants