Skip to content

Inconsistency between classic StyleCop and StyleCopAnalyzers with SA1303 #2082

Closed as not planned

Description

@NikolayIT

In this code:

public class SomeClass
{
    public void SomeMethod()
    {
        const string url = "some constant value";
    }
}

the classic StyleCop reports SA1303 (ConstFieldNamesMustBeginWithUpperCaseLetter) but StyleCopAnalyzers doesn't.

Activity

  1. vweijsters commented on Feb 11, 2016

    @vweijsters
    Contributor

    This is an interesting deviation from StyleCop Classic. The url declaration is a local constant, which is technically not a field. I view this more like a local variable declaration, which should start with a lowercase letter. My preference would be to document this as a known change.

    I'll label this needs discussion to hear more opinions.

  2. sharwell commented on Feb 11, 2016

    @sharwell
    Member

    ❓ Does our existing analyzer for local variables report a warning for const string Url = ...?

  3. vweijsters commented on Feb 12, 2016

    @vweijsters
    Contributor

    @sharwell No, it doesn't. Its explicitly excluded in the SA1312 analyzer (see link)

  4. lukas-ais commented on Mar 3, 2016

    @lukas-ais
    Contributor

    You are right, @vweijsters. The rule 1303 is named ConstFieldNamesMustBeginWithUpperCaseLetter. So it should only define the behavior for (constant) fields.
    But the real life interpretation seems already Const*NamesMustBeginWithUpperCaseLetter. Further I think, why should constant variables be named identically to "normal" ones?
    For me, it would be OK to change the definition 1303 to cover constant variable, too.
    Alternatively a new rule ConstVariableNamesMustBeginWithUpperCaseLetter should be introduced.

  5. khmurach commented on Jul 7, 2017

    @khmurach

    Any update here?

  6. sharwell commented on Jul 7, 2017

    @sharwell
    Member

    No update currently. Rather than create a new rule I'd rather update SA1303 to cover this case, but it seems low priority unless someone volunteers to implement it.

  7. khmurach commented on Jul 7, 2017

    @khmurach

    I would like to help. May I?

  8. sharwell commented on Jul 7, 2017

    @sharwell
    Member

    @khmurach All yours 👍 Let me know if you have questions.

    💡 This is much easier than SA1312 to implement, because const variables can't be declared in all the places where other variables can.

  9. khmurach commented on Jul 10, 2017

    @khmurach

    Actually it's not a bug. I was receiving SA1303 error for local camelCase const because I was using native StyleCop.MsBuild. But I don't like to use PascalCase for local consts. Then I started googling why StyleCop rise this error for local consts when rule name is ConstField. Then I found this thread and didn't get that it's about completely different tool. And today I've realized that current behavior of StyleCopAnalyzers is what I was looking for. @vweijsters, you are right in your comment: local const is not a field and should not be in PascalCase.

    Sorry for confusion.

    I've created a unit test for local const. May I create pull request for it?

  10. sharwell commented on Nov 29, 2025

    @sharwell
    Member

    At this point, changing SA1303 to cover local variables would produce a large amount of code churn in downstream projects that just doesn't seem worth it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions