Skip to content

Commit dfeef6d

Browse files
authored
Handle extension members by 'make member static' (#7739)
* Handle extension members by 'make member static' * Suppress warning introduced by Roslyn bump * Suppress warning in test * Update tests * Update Roslyn To bring in dotnet/roslyn#79571. * Update Roslyn To bring in dotnet/roslyn#79576.
1 parent 1551bee commit dfeef6d

File tree

9 files changed

+64
-18
lines changed

9 files changed

+64
-18
lines changed

src/Microsoft.CodeAnalysis.NetAnalyzers/src/Microsoft.CodeAnalysis.NetAnalyzers/Microsoft.CodeQuality.Analyzers/QualityGuidelines/MarkMembersAsStatic.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ void OnOperationBlockStart(OperationBlockStartAnalysisContext context)
130130
// This member cannot be made static.
131131
isInstanceReferenced = true;
132132
}
133+
else if (operation.Parameter.ContainingSymbol is ITypeSymbol { TypeKind: ITypeSymbolExtensions.ExtensionTypeKind })
134+
{
135+
// We're referencing an extension receiver.
136+
isInstanceReferenced = true;
137+
}
133138
}, OperationKind.ParameterReference);
134139

135140
context.RegisterOperationBlockEndAction(context =>

src/Microsoft.CodeAnalysis.NetAnalyzers/src/Utilities/Compiler/Extensions/ITypeSymbolExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ namespace Analyzer.Utilities.Extensions
1212
{
1313
internal static class ITypeSymbolExtensions
1414
{
15+
public const TypeKind ExtensionTypeKind = (TypeKind)14;
16+
1517
#if CODEANALYSIS_V3_OR_BETTER
1618
public static bool IsAssignableTo(
1719
[NotNullWhen(returnValue: true)] this ITypeSymbol? fromSymbol,

src/Microsoft.CodeAnalysis.NetAnalyzers/tests/Microsoft.CodeAnalysis.NetAnalyzers.UnitTests/Microsoft.CodeQuality.Analyzers/ApiDesignGuidelines/DefineAccessorsForAttributeArgumentsTests.Fixer.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@ public InternalGetterTestAttribute(string name)
153153
public string Name
154154
{
155155
get { return m_name; }
156-
157156
internal set { m_name = value; }
158157
}
159158
}");
@@ -198,7 +197,6 @@ public InternalGetterTestAttribute(string name)
198197
public string Name
199198
{
200199
get { return m_name; }
201-
202200
internal set { m_name = value; }
203201
}
204202
}");
@@ -285,7 +283,6 @@ public PublicSetterTestAttribute(string name)
285283
public string Name
286284
{
287285
get { return m_name; }
288-
289286
internal set { m_name = value; }
290287
}
291288
}");

src/Microsoft.CodeAnalysis.NetAnalyzers/tests/Microsoft.CodeAnalysis.NetAnalyzers.UnitTests/Microsoft.CodeQuality.Analyzers/ApiDesignGuidelines/ImplementStandardExceptionConstructorsTests.Fixer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.
1+
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.
22

33
using System.Threading.Tasks;
44
using Xunit;

src/Microsoft.CodeAnalysis.NetAnalyzers/tests/Microsoft.CodeAnalysis.NetAnalyzers.UnitTests/Microsoft.CodeQuality.Analyzers/ApiDesignGuidelines/OperatorOverloadsHaveNamedAlternatesTests.Fixer.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,6 @@ public class C
206206
public class C
207207
{
208208
public static C operator +(C left, C right) { return new C(); }
209-
210209
public static C Add(C left, C right) { return new C(); }
211210
}
212211
",
@@ -235,7 +234,6 @@ public class C
235234
{
236235
public static bool operator true(C item) { return true; }
237236
public static bool operator false(C item) { return false; }
238-
239237
public bool IsTrue => true;
240238
}
241239
",

src/Microsoft.CodeAnalysis.NetAnalyzers/tests/Microsoft.CodeAnalysis.NetAnalyzers.UnitTests/Microsoft.CodeQuality.Analyzers/ApiDesignGuidelines/OperatorsShouldHaveSymmetricalOverloadsTests.Fixer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.
1+
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.
22

33
using System.Threading.Tasks;
44
using Xunit;

src/Microsoft.CodeAnalysis.NetAnalyzers/tests/Microsoft.CodeAnalysis.NetAnalyzers.UnitTests/Microsoft.CodeQuality.Analyzers/ApiDesignGuidelines/OverrideEqualsAndOperatorEqualsOnValueTypesTests.Fixer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.
1+
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.
22

33
using System.Threading.Tasks;
44
using Xunit;

src/Microsoft.CodeAnalysis.NetAnalyzers/tests/Microsoft.CodeAnalysis.NetAnalyzers.UnitTests/Microsoft.CodeQuality.Analyzers/QualityGuidelines/MarkMembersAsStaticTests.cs

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,7 +1322,7 @@ public string P3
13221322
get;
13231323
}
13241324
1325-
public string [|P4|] // Because of the error there is no generated field
1325+
public string P4 // Because of the error there is no generated field
13261326
{
13271327
[DebuggerStepThrough]
13281328
{|CS8051:set|};
@@ -1500,7 +1500,7 @@ public static void Recursive(string argument)
15001500
}.RunAsync();
15011501
}
15021502

1503-
[Fact(Skip = "Need update of roslyn to parse primary constructors properly"), WorkItem(6573, "https://github.com/dotnet/roslyn-analyzers/issues/6573")]
1503+
[Fact, WorkItem(6573, "https://github.com/dotnet/roslyn-analyzers/issues/6573")]
15041504
public Task PrimaryConstructor()
15051505
{
15061506
return new VerifyCS.Test
@@ -1529,5 +1529,53 @@ public class Student(int id, string name, IEnumerable<decimal> grades)
15291529
LanguageVersion = LanguageVersion.Preview
15301530
}.RunAsync();
15311531
}
1532+
1533+
[Fact, WorkItem(78858, "https://github.com/dotnet/roslyn/issues/78858")]
1534+
public Task ExtensionMembers_Instance()
1535+
{
1536+
return new VerifyCS.Test
1537+
{
1538+
TestCode = """
1539+
public static class E
1540+
{
1541+
extension(int x)
1542+
{
1543+
public int M() => x + 1;
1544+
public int P => x + 1;
1545+
}
1546+
}
1547+
""",
1548+
LanguageVersion = LanguageVersion.Preview,
1549+
}.RunAsync();
1550+
}
1551+
1552+
[Fact, WorkItem(78858, "https://github.com/dotnet/roslyn/issues/78858")]
1553+
public Task ExtensionMembers_Static()
1554+
{
1555+
return new VerifyCS.Test
1556+
{
1557+
TestCode = """
1558+
public static class E
1559+
{
1560+
extension(int x)
1561+
{
1562+
public int [|M|]() => 1;
1563+
public int [|P|] => 2;
1564+
}
1565+
}
1566+
""",
1567+
FixedCode = """
1568+
public static class E
1569+
{
1570+
extension(int x)
1571+
{
1572+
public static int M() => 1;
1573+
public static int P => 2;
1574+
}
1575+
}
1576+
""",
1577+
LanguageVersion = LanguageVersion.Preview,
1578+
}.RunAsync();
1579+
}
15321580
}
15331581
}

src/Microsoft.CodeAnalysis.NetAnalyzers/tests/Microsoft.CodeAnalysis.NetAnalyzers.UnitTests/Microsoft.NetCore.Analyzers/InteropServices/DynamicInterfaceCastableImplementationTests.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -586,8 +586,7 @@ interface I2 : I
586586
{
587587
void I.Method() {}
588588
589-
private static void Method2(I2 @this)
590-
{ }
589+
private static void Method2(I2 @this) { }
591590
}";
592591

593592
await VerifyCSCodeFixAsync(source, codeFix);
@@ -787,8 +786,7 @@ interface I2 : I
787786
{
788787
void I.Method() {}
789788
790-
public static void Method2(I2 @this)
791-
{ }
789+
public static void Method2(I2 @this) { }
792790
}";
793791

794792
await VerifyCSCodeFixAsync(source, codeFix);
@@ -826,8 +824,7 @@ interface I2 : I
826824
{
827825
void I.Method() {}
828826
829-
public static void Method2(I2 @this)
830-
{ }
827+
public static void Method2(I2 @this) { }
831828
}";
832829

833830
await VerifyCSCodeFixAsync(source, codeFix);
@@ -865,8 +862,7 @@ interface I2 : I
865862
{
866863
void I.Method() {}
867864
868-
protected static void Method2(I2 @this)
869-
{ }
865+
protected static void Method2(I2 @this) { }
870866
}";
871867

872868
await VerifyCSCodeFixAsync(source, codeFix);

0 commit comments

Comments
 (0)