dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
19.04k stars 4.03k forks source link

Codestyle: disallow same variable name with different casing #38531

Open kendrahavens opened 5 years ago

kendrahavens commented 5 years ago

Original devcomm ticket

Have a setting to allow/disallow variables with same name but different letter cases

Example

I accidentally had two namespaces, GpsData and GPSData, and it allowed them both and I couldn't find the classes of the one in the other, thinking there was only one namespace. Same with variables.

markus27183 commented 5 years ago

This is a great idea.

CyrusNajmabadi commented 5 years ago

This can be done by users writing their own analyzer.

markus27183 commented 5 years ago

Yes I can also write my own operating system, or my own IDE or runtime environment. But if you want a customer like me to like your product more you will add this feature.

On Sat, Sep 7, 2019 at 12:56 AM CyrusNajmabadi notifications@github.com wrote:

This can be done by users writing their own analyzer.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/dotnet/roslyn/issues/38531?email_source=notifications&email_token=ACSL73ELE7TAHGOEF2L7OBLQILNZDA5CNFSM4IUDS7O2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD6EIEAY#issuecomment-529039875, or mute the thread https://github.com/notifications/unsubscribe-auth/ACSL73BLDCVTMJTLCQ7ACADQILNZDANCNFSM4IUDS7OQ .

CyrusNajmabadi commented 5 years ago

@markus27183 i'd be happy to walk you through creating such an analyzer. With that info you could then add additional analyzers in the future if you run into more stuff you want to check for (or you can change this particular analyzer if you run into cases you don't want it to trigger on).

LMK!

sharwell commented 4 years ago

Design review conclusion: We aren't sure what the "right" approach here is, so we're interested in feedback from users regarding exactly how this analysis would be expected to behave. Some items to consider:

  1. CLS compliance does already check that exposed names do not differ only by case

  2. An FXCop analyzer existed for this, but was not ported to FXCop Analyzers in dotnet/roslyn-analyzers

  3. We need to be careful about code styles that intentionally use names that differ only by case, such as the following naming for properties with backing fields:

    private int propertyValue;
    public int PropertyValue {
      get => propertyValue;
      set => propertyValue = value;
    }
  4. Some possibilities for an analyzer are:

    • Only check namespaces, or perhaps namespaces and types
    • Only check names defined in the same scope
    • Check names defined in the same scope, but intentionally omit warnings for the field/property naming seen above

Other options certainly exist.