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

SA1010 incorrectly reported on opening bracket in C# 11 list pattern #3503

Closed
jnm2 opened this issue May 11, 2022 · 6 comments · Fixed by #3507
Closed

SA1010 incorrectly reported on opening bracket in C# 11 list pattern #3503

jnm2 opened this issue May 11, 2022 · 6 comments · Fixed by #3507

Comments

@jnm2
Copy link
Contributor

jnm2 commented May 11, 2022

// ⚠️ SA1010 Opening square brackets should not be preceded by a space
//                 ↓
if (new[] { 1 } is [1])
{
}

SDK 6.0.200 or newer required. Example csproj:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <LangVersion>preview</LangVersion>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.435">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
  </ItemGroup>

</Project>
@bjornhellander
Copy link
Contributor

@jnm2, can you point to a large public repository that uses patterns extensively and also uses stylecop? Would be good for testing, to see that at least all existing problems in that repo gets fixed before releasing the next beta.

@jnm2
Copy link
Contributor Author

jnm2 commented Apr 15, 2023

@bjornhellander Sorry, not off the top of my head.

@alexrp
Copy link
Contributor

alexrp commented Apr 15, 2023

FWIW, I use patterns a fair amount in some of my projects and have quite a few StyleCop rules disabled due to bugs (some relating to patterns, some not).

@bjornhellander
Copy link
Contributor

@jnm2 No problem!
@alexrp Excellent. I will have a look. Thanks!

@AArnott
Copy link
Contributor

AArnott commented Apr 23, 2023

It shows up in pattern matching within switch expressions as well:

    internal static TransparentAddress? TryParse(ReadOnlySpan<char> address)
    {
        if (address.Length < 2)
        {
            return null;
        }

        Span<byte> decoded = stackalloc byte[DecodedLength];
        Base58Check.Decode(address, decoded);
        return decoded[..2] switch
        {
            [0x1c, 0xb8] or [0x1d, 0x25] => new TransparentP2PKHAddress(address),
            [0x1c, 0xbd] or [0x1C, 0xBA] => new TransparentP2SHAddress(address),
            _ => null,
        };
    }

In the above snippet, SA1010 appears on the [ of [0x1d and [0x1C.

@bjornhellander
Copy link
Contributor

Thanks for the example, @AArnott. The existing pull request should handle this case as well, if it gets merged.

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

Successfully merging a pull request may close this issue.

5 participants