Skip to content

SA1206 firing incorrectly for required in C# 11 #3527

Closed

Description

@stephentoub

Version:
1.2.0-beta.435

Repro:

internal struct SomeStruct
{
    public required int Prop { get; set; }
}

Expected:
No warnings

Actual:

SA1206: The 'required' modifier should appear before 'public'

Activity

  1. bjornhellander commented on Jun 19, 2022

    @bjornhellander
    Contributor

    I also notice that the code fix bails out and does nothing if applied to an unknown modifier. Would be nice if the analyzer did the same, to avoid these endless false positives as the language evolves. Not a big problem with this particular analyzer, but while someone fixes the false positive I think it would be good if the analyzer could be redesigned at the same time.

  2. bjornhellander commented on Jun 19, 2022

    @bjornhellander
    Contributor

    I tried to create a unit test case, but fail to make the tested code compile:
    error CS1519: Invalid token 'int' in class, record, struct, or interface member declaration

    Could for example be because Microsoft.CodeAnalysis.CSharp.Workspaces 4.3.0-1.final doesn't support this syntax, or maybe we need to configure the tested project to use reference assemblies for .net 7.0?

  3. stephentoub commented on Jun 19, 2022

    @stephentoub
    Author

    doesn't support this syntax

    You'll need a really recent build of the C# 11 compiler, e.g. in the last two weeks or so.

  4. bjornhellander commented on Jun 19, 2022

    @bjornhellander
    Contributor

    The latest code analysis package was from a couple of days ago, but I don't know how recent the actual code is. Let´s wait for the next beta and try again.

  5. bjornhellander commented on Jul 17, 2022

    @bjornhellander
    Contributor

    I have a fix that I am almost happy with (using version 4.3.0-2.final of Roslyn), except that it causes some tests for 1503, 1519 and 1520 to fail due to changed formatting behaviour in the new Roslyn version. I have confirmed that these regressions happen even if I only update that package, so it's not related to the changes for the new modifier.

    All failing tests are called TestCodeFixProviderWithAlternateIndentationAsync and they only fail in the c# 11 test project, which is the one with the new Roslyn version. All tests use the SA1503CodeFixProvider to add missing braces using the method SyntaxFactory.Block().

    Xunit.Sdk.TrueException: 'Context: Iterative code fix application
    content of '/0/Test0.cs' did not match. Diff shown with expected as baseline:
     using System.Diagnostics;
     public class Foo
     {
      public void Bar(int i)
      {
       if (i == 0)
    -  {
    -   Debug.Assert(true);
    -  }
    - }
    +        {
    +            Debug.Assert(true);
    +        }
    +    }
     }
    
    Expected: True
    Actual:   False'
    

    Do you have any insights on this, @sharwell?

  6. bjornhellander commented on Jul 29, 2022

    @bjornhellander
    Contributor

    On hold until a fix for the formatting is available: dotnet/roslyn#63013

  7. Brunni commented on Nov 21, 2022

    @Brunni

    Are there any news about the required modifier? I get "indention errors" here.

  8. usausa commented on Nov 30, 2022

    @usausa

    I am also waiting for this issue to be resolved.

  9. Malgefor commented on Jan 17, 2023

    @Malgefor

    Any update on this issue?

  10. bjornhellander commented on Jan 30, 2023

    @bjornhellander
    Contributor

    @Malgefor, there is a pull request prepared, but we are having problems with a bug in the Roslyn version needed to add tests for this change. I have created #3580 to (as a first step) just update the Roslyn version with suggestions on how to temporarily handle the Roslyn bug.

    How do you want to handle this, @sharwell?

  11. sharwell commented on Jan 30, 2023

    @sharwell
    Member

    It's a bit complicated. I need to update dotnet/roslyn-sdk so we can update that dependency, but that work has been blocked on updates to microsoft/vs-extension-testing. Hopefully back in a good state soon.

  12. added this to the 1.2-beta.next milestone on Apr 11, 2023
  13. tzachihakmon commented on Feb 24, 2025

    @tzachihakmon

    Hey im with version 1.2.0-beta.556 and its not working yet, any news?

  14. bjornhellander commented on Feb 24, 2025

    @bjornhellander
    Contributor

    @tzachihakmon As far as I understand, this was fixed in 1.2.0-beta.507. Create a new issue if it's not working.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions