Skip to content

[Proposal] Analyzer that suggests ToUpperInvariant/ToLowerInvariant in place of ToUpper/ToLower #43976

Closed
dotnet/roslyn-analyzers
#2186

Description

@jamesqo

When using String.ToUpper() or String.ToLower(), suggest using String.ToUpperInvariant() and String.ToLowerInvariant().

Something similar is already in place here, but for string comparisons: CA1309: Use ordinal StringComparison

A PR already exists to fix this. We want to get this proposal approved before reviewing it: dotnet/roslyn-analyzers#2186

  • Analyzer: small
  • Fixer: small
  • Suggested category: Globalization
  • Suggested severity: Info (to match CA1309)
  • Milestone: 6.0

Usage examples:

string s = "my string";
string lower, upper;


// Suggest to use ToLowerInvariant() or to explicitly indicate the culture.
// Offer fix to use ToLowerInvariant()
lower = s.ToLower();
lower = "str".ToLower();

// Suggest to use ToUpperInvariant() or to explicitly indicate the culture.
// Offer fix to use ToUpperInvariant()
upper = s.ToUpper();
upper = "str".ToUpper();

CultureInfo culture = new CultureInfo("es-ES", false);

// Should not suggest
lower = s.ToLower(culture);
upper = s.ToUpper(culture);



// Should we suggest to use To*Invariant() in these cases? tarekgh thinks it's not necessary, but we can discuss it:
// Is this the same as ToLowerInvariant? https://source.dot.net/#System.Private.CoreLib/String.Manipulation.cs,1710
lower = s.ToLower(CultureInfo.InvariantCulture);
// Is this the same as ToUpperInvariant? https://source.dot.net/#System.Private.CoreLib/String.Manipulation.cs,1725
upper = s.ToUpper(CultureInfo.InvariantCulture);

Activity

  1. kharai-tor commented on Mar 10, 2019

    @kharai-tor

    @mavasani created a PR for this - dotnet/roslyn-analyzers#2186

  2. carlossanlop commented on Oct 28, 2020

    @carlossanlop
    Contributor

    @mavasani should this go through API review in the runtime repo, or is this just an improvement to an existing analyzer?

  3. carlossanlop commented on Oct 28, 2020

    @carlossanlop
    Contributor

    @mavasani This issue was opened back in 2017 before we decided that Roslyn analyzer proposals should be approved in the API review meeting in the runtime repo. It already has a PR opened back in 2019, but it wasn't discussed here or approved. Should we move it to the runtime repo to approve it first?

  4. mavasani commented on Oct 28, 2020

    @mavasani

    Yes, I agree we need a proposal/triage in runtime repo before taking this forward.

  5. added
    untriaged
    on Oct 28, 2020
  6. Dotnet-GitSync-Bot commented on Oct 28, 2020

    @Dotnet-GitSync-Bot
    Collaborator

    I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

  7. added
    api-suggestion
    code-analyzer
    code-fixer
    and removed
    untriaged
    on Oct 28, 2020
  8. added this to the 6.0.0 milestone on Oct 29, 2020
  9. tarekgh commented on Oct 29, 2020

    @tarekgh
    Member

    @jamesqo as suggested in this thread you need to add the proposal up in the issue description.

    are you suggesting to have the analyzer suggest ToUpperInvariant/ToLowerInvariant all the time when ToUpper/ToLower show up? or would be depending on the context of the callsite?

  10. 13 remaining items

  11. modified the milestones: 6.0.0, 7.0.0 on Jul 19, 2021
  12. tarekgh commented on Jul 19, 2021

    @tarekgh
    Member

    I moved this to 7.0 as I am not seeing urgency to have it for 6.0. That is fine if we get a PR for 6.0 too.

  13. kharai-tor commented on Jul 20, 2021

    @kharai-tor

    @tarekgh I won't have any time to work on this in the next 2 months. I'll come back to it afterwards if that works for you guys.

  14. carlossanlop commented on Sep 14, 2021

    @carlossanlop
    Contributor

    @TKharaishvili are you still going to work on this?

  15. kharai-tor commented on Sep 14, 2021

    @kharai-tor

    @carlossanlop yes, I'll have an updated PR over the next several days.

  16. kharai-tor commented on Sep 16, 2021

    @kharai-tor

    Hi @carlossanlop I tested positive for covid and won't be able to get any work done for some time. If you guys need this taken care of urgently, please unassign me from the issue, otherwise I'll get back to it once I'm better.
    I apologize for the inconvenience.

  17. kharai-tor commented on Oct 3, 2021

    @kharai-tor

    Hi @carlossanlop I'm back at it. I see a lot has changed in the repo since I first sent that PR(which is to be expected for a 3 year gap). Originally I added the analyzer source file to the Microsoft.NetCore.Analyzers project. That project is no longer there but I found something similar, I'm thinking the Microsoft.CodeAnalysis.NetAnalyzers project -> Microsoft.NetCore.Analyzers.Runtime folder is the right place for it. Would you agree?

    Also I'm thinking of dropping this current PR and creating a new one instead. Doing a merge after this long is proving to be too much of a headache.

  18. tarekgh commented on Jan 12, 2022

    @tarekgh
    Member

    @carlossanlop @buyaa-n could you please help with @TKharaishvili question in the previous comment?

    @TKharaishvili are you still willing to submit the PR? Thanks!

  19. buyaa-n commented on Jan 13, 2022

    @buyaa-n
    Contributor

    Sorry for late reply @TKharaishvili @tarekgh

    I'm thinking the Microsoft.CodeAnalysis.NetAnalyzers project -> Microsoft.NetCore.Analyzers.Runtime folder is the right place for it. Would you agree?

    Yes that is the right place

    Also I'm thinking of dropping this current PR and creating a new one instead. Doing a merge after this long is proving to be too much of a headache.

    Right, sounds good, feel free to close your old PR then

  20. kharai-tor commented on Jan 13, 2022

    @kharai-tor

    @tarekgh @buyaa-n I already proceeded since there was no response for a while. Decided to update the existing PR, it currently has the analyzer in it and I believe it was looked at by some of the team members. All that's missing right now is a fixer. I'll resume working on that next week.

  21. danmoseley commented on Apr 13, 2022

    @danmoseley
    Contributor

    @TKharaishvili hello, are you still working on this ?

  22. kharai-tor commented on Apr 13, 2022

    @kharai-tor

    @danmoseley I pushed the latest code review requested changes yesterday. Waiting for another round of review now.

  23. ghost locked as resolved and limited conversation to collaborators on May 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api-approvedarea-System.Globalizationcode-analyzercode-fixer

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions