Skip to content

Commit a99b1c1

Browse files
committed
Update SA1101 tests for extended property patterns
1 parent 4d92a69 commit a99b1c1

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp10/ReadabilityRules/SA1101CSharp10UnitTests.cs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,63 @@ public bool Method(Test arg)
3131

3232
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
3333
}
34+
35+
[Fact]
36+
[WorkItem(3984, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3984")]
37+
public async Task TestExtendedPropertyPatternInSwitchAsync()
38+
{
39+
var testCode = @"public class Test
40+
{
41+
public Test Child { get; init; }
42+
public int Value { get; init; }
43+
44+
public int Evaluate(Test other)
45+
{
46+
return other switch
47+
{
48+
{ Child.Value: > 0 } and not { Value: 0 } => 1,
49+
{ Child.Value: <= 0 } or ({ Value: 0 } and { Child.Value: 0 }) => 0,
50+
_ => -1,
51+
};
52+
}
53+
}";
54+
55+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
56+
}
57+
58+
[Fact]
59+
[WorkItem(3984, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3984")]
60+
public async Task TestExtendedPropertyPatternWithInstanceMemberAccessAsync()
61+
{
62+
var testCode = @"public class Test
63+
{
64+
private int value;
65+
66+
public Test Child { get; }
67+
public int Value { get; }
68+
69+
public bool Evaluate(Test other)
70+
{
71+
return other is { Child.Value: > 0 } && {|#0:value|} > 0;
72+
}
73+
}";
74+
75+
var fixedCode = @"public class Test
76+
{
77+
private int value;
78+
79+
public Test Child { get; }
80+
public int Value { get; }
81+
82+
public bool Evaluate(Test other)
83+
{
84+
return other is { Child.Value: > 0 } && this.value > 0;
85+
}
86+
}";
87+
88+
var expected = Diagnostic().WithLocation(0);
89+
90+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
91+
}
3492
}
3593
}

documentation/SA1101.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ By default, StyleCop disallows the use of underscores or *m_* to mark local clas
2727

2828
A final advantage of using the 'this.' prefix is that typing *this.* will cause Visual Studio to show the IntelliSense popup, making it quick and easy for the developer to choose the class member to call.
2929

30+
When using property patterns, including the extended property pattern syntax introduced in C# 10, the rule does not require (or allow) `this.` to qualify the properties referenced by the pattern. These expressions refer to members of the value being matched rather than the containing type. For example, no SA1101 diagnostic is reported for the following:
31+
32+
```csharp
33+
if (item is { Outer.Inner: value })
34+
{
35+
}
36+
```
37+
3038
## How to fix violations
3139

3240
To fix a violation of this rule, insert the 'this.' prefix before the call to the class member.

0 commit comments

Comments
 (0)