question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue
// ⚠️ 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>

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:7
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
bjornhellandercommented, Apr 24, 2023

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

0reactions
AArnottcommented, 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

SA1010 incorrectly reported on opening bracket in C# 11 ...
FWIW, I use patterns a fair amount in some of my projects and have quite a few StyleCop rules disabled due to bugs...
Read more >
Pattern checking.check of incorrect number of operators ...
Example 1: (a+b)- has a one extra operator How do i confirm that the number of operators are incorrect. Example 2 : ((a+b)/(c-d)...
Read more >
.NET Programming Standards & Reference Guide Version ...
An opening curly bracket within a C# element, statement, or expression is followed by a blank line. Rule Description. To improve the readability...
Read more >
What's new in C# 11 - C# Guide
Get an overview of the new features added in C# 11. ... UTF-8 string literals; Newlines in string interpolation expressions; List patterns ......
Read more >
Pattern matching using the is and switch expressions.
Discard pattern: to match any expression. List patterns: to test if sequence elements match corresponding nested patterns. Introduced in C# 11.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found