-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Introduce local variable supporting target-type new syntax #76342
Merged
CyrusNajmabadi
merged 6 commits into
dotnet:main
from
MartyIX:feature/2024-12-09-TargetTypeNew-for-IntroduceLocalVariable
Jan 2, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
42634bd
First attempt
MartyIX 934aff0
Address review comments + fix failing tests
MartyIX 7f2db9d
Merge remote-tracking branch 'upstream/main' into feature/2024-12-09-…
CyrusNajmabadi 99d5122
PR feedback
CyrusNajmabadi 364e938
Add test demonstreating old behavior
CyrusNajmabadi 523eab5
fix test
CyrusNajmabadi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -935,13 +935,63 @@ class Program | |
{ | ||
static void Main() | ||
{ | ||
G<int>.@class {|Rename:@class|} = new G<int>.@class(); | ||
G<int>.@class {|Rename:@class|} = new(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add sibling tests that have the same input, but which have the ImplicitObjectCreationWhenTypeIsApparent set to false, to show we get the old output in that case? |
||
G<int>.Add(@class); | ||
} | ||
} | ||
"""); | ||
} | ||
|
||
[Fact] | ||
public async Task TestNameVerbatimIdentifier1NoVar_1() | ||
{ | ||
await TestInRegularAndScriptAsync( | ||
""" | ||
static class G<T> | ||
{ | ||
public class @class | ||
{ | ||
} | ||
|
||
public static void Add(object @class) | ||
{ | ||
} | ||
} | ||
|
||
class Program | ||
{ | ||
static void Main() | ||
{ | ||
G<int>.Add([|new G<int>.@class()|]); | ||
} | ||
} | ||
""", | ||
""" | ||
static class G<T> | ||
{ | ||
public class @class | ||
{ | ||
} | ||
|
||
public static void Add(object @class) | ||
{ | ||
} | ||
} | ||
|
||
class Program | ||
{ | ||
static void Main() | ||
{ | ||
G<int>.@class {|Rename:@class|} = new G<int>.@class(); | ||
G<int>.Add(@class); | ||
} | ||
} | ||
""", options: new(GetLanguage()) | ||
{ | ||
{ CSharpCodeStyleOptions.ImplicitObjectCreationWhenTypeIsApparent, CodeStyleOption2.FalseWithSilentEnforcement } | ||
}); | ||
} | ||
|
||
[Fact] | ||
public async Task TestNameVerbatimIdentifier2() | ||
{ | ||
|
@@ -1018,7 +1068,7 @@ public static void Add(object @class) | |
|
||
static void Main() | ||
{ | ||
G<int>.@class {|Rename:@class|} = new G<int>.@class(); | ||
G<int>.@class {|Rename:@class|} = new(); | ||
G<int>.Add(@class); | ||
} | ||
} | ||
|
@@ -3001,7 +3051,7 @@ class Program | |
{ | ||
static void Main() | ||
{ | ||
Nullable<int*> {|Rename:v|} = new Nullable<int*>(); | ||
Nullable<int*> {|Rename:v|} = new(); | ||
v.GetValueOrDefault(); | ||
} | ||
} | ||
|
@@ -4283,6 +4333,56 @@ public T this[int i] | |
await TestInRegularAndScriptAsync(code, expected, options: ImplicitTypingEverywhere()); | ||
} | ||
|
||
[Fact] | ||
public async Task TestIntroduceLocalWithTargetTypedNew() | ||
{ | ||
var code = | ||
""" | ||
using System; | ||
class SampleType | ||
{ | ||
public SampleType() | ||
{ | ||
int sum = Sum([|new Numbers()|]); | ||
} | ||
|
||
private int Sum(Numbers numbers) | ||
{ | ||
return 42; | ||
} | ||
|
||
private class Numbers {} | ||
} | ||
"""; | ||
|
||
var expected = | ||
""" | ||
using System; | ||
class SampleType | ||
{ | ||
public SampleType() | ||
{ | ||
Numbers {|Rename:numbers|} = new(); | ||
int sum = Sum(numbers); | ||
} | ||
|
||
private int Sum(Numbers numbers) | ||
{ | ||
return 42; | ||
} | ||
|
||
private class Numbers {} | ||
} | ||
"""; | ||
|
||
OptionsCollection optionsCollection = new(GetLanguage()) | ||
{ | ||
{ CSharpCodeStyleOptions.ImplicitObjectCreationWhenTypeIsApparent, new CodeStyleOption2<bool>(true, NotificationOption2.Warning) }, | ||
MartyIX marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
|
||
await TestInRegularAndScriptAsync(code, expected, options: optionsCollection); | ||
} | ||
|
||
[Fact] | ||
public async Task TestIntroduceFieldInExpressionBodiedPropertyGetter() | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you removed the trivia from expression when making updatedExpression at the top of the method. but not here. consider either doign the same here, or doign
updatedExpression is ObjectCreationExpressionSyntax ...
to ensure that original op wasn't lost.