Skip to content

Commit adc9033

Browse files
Do not offer add parameter on a type that cannot become a parameter (#79060)
2 parents 414bc29 + ce706bb commit adc9033

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

src/Features/CSharpTest/IntroduceParameter/IntroduceParameterTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2220,4 +2220,21 @@ public void Test(string s)
22202220

22212221
await TestMissingInRegularAndScriptAsync(code);
22222222
}
2223+
2224+
[Fact]
2225+
public async Task TestNotOnNamedType1()
2226+
{
2227+
await TestMissingInRegularAndScriptAsync(
2228+
"""
2229+
using System;
2230+
2231+
class C
2232+
{
2233+
void M()
2234+
{
2235+
[||]Console.WriteLine();
2236+
}
2237+
}
2238+
""");
2239+
}
22232240
}

src/Features/CSharpTest/IntroduceVariable/IntroduceVariableTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9568,4 +9568,32 @@ class C
95689568
parseOptions: null,
95699569
index: 1);
95709570
}
9571+
9572+
[Fact]
9573+
public async Task TestNotOnNamedType1()
9574+
{
9575+
await TestMissingInRegularAndScriptAsync(
9576+
"""
9577+
using System;
9578+
9579+
class C
9580+
{
9581+
void M()
9582+
{
9583+
[||]Console.WriteLine();
9584+
}
9585+
}
9586+
""");
9587+
}
9588+
9589+
[Fact]
9590+
public async Task TestNotOnNamedType2()
9591+
{
9592+
await TestMissingInRegularAndScriptAsync(
9593+
"""
9594+
using System;
9595+
9596+
[||]Console.WriteLine();
9597+
""");
9598+
}
95719599
}

src/Features/Core/Portable/IntroduceParameter/AbstractIntroduceParameterCodeRefactoringProvider.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ public sealed override async Task ComputeRefactoringsAsync(CodeRefactoringContex
7878
if (expressionSymbol is IParameterSymbol parameterSymbol && parameterSymbol.ContainingSymbol.Equals(containingSymbol))
7979
return;
8080

81+
// Direct reference to named type or type parameter. e.g. `$$Console.WriteLine()` or `T.Add(...)`. These
82+
// are effectively statics (not values) and cannot become parameter.
83+
if (expressionSymbol is INamedTypeSymbol or ITypeParameterSymbol)
84+
return;
85+
8186
// Code actions for trampoline and overloads will not be offered if the method is a constructor.
8287
// Code actions for overloads will not be offered if the method if the method is a local function.
8388
var methodKind = methodSymbol.MethodKind;

0 commit comments

Comments
 (0)