Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
1df59ddc
R
roslyn
项目概览
lwm1986
/
roslyn
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
roslyn
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
1df59ddc
编写于
11月 21, 2019
作者:
C
Cyrus Najmabadi
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Properly respect 'var' settings when generating equals/GetHashCode
上级
59cd90fd
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
200 addition
and
59 deletion
+200
-59
src/EditorFeatures/CSharpTest/GenerateFromMembers/GenerateEqualsAndGetHashCodeFromMembers/GenerateEqualsAndGetHashCodeFromMembersTests.cs
...omMembers/GenerateEqualsAndGetHashCodeFromMembersTests.cs
+156
-53
src/Workspaces/CSharp/Portable/CodeGeneration/CSharpSyntaxGenerator.cs
...s/CSharp/Portable/CodeGeneration/CSharpSyntaxGenerator.cs
+6
-0
src/Workspaces/Core/Portable/Editing/SyntaxGenerator.cs
src/Workspaces/Core/Portable/Editing/SyntaxGenerator.cs
+8
-0
src/Workspaces/Core/Portable/Shared/Extensions/SyntaxGeneratorExtensions_CreateEqualsMethod.cs
...xtensions/SyntaxGeneratorExtensions_CreateEqualsMethod.cs
+4
-2
src/Workspaces/Core/Portable/Shared/Extensions/SyntaxGeneratorExtensions_CreateGetHashCodeMethod.cs
...ions/SyntaxGeneratorExtensions_CreateGetHashCodeMethod.cs
+20
-4
src/Workspaces/VisualBasic/Portable/CodeGeneration/VisualBasicSyntaxGenerator.vb
...sic/Portable/CodeGeneration/VisualBasicSyntaxGenerator.vb
+6
-0
未找到文件。
src/EditorFeatures/CSharpTest/GenerateFromMembers/GenerateEqualsAndGetHashCodeFromMembers/GenerateEqualsAndGetHashCodeFromMembersTests.cs
浏览文件 @
1df59ddc
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using
System.Collections.Generic
;
using
System.Linq
;
using
System.Threading.Tasks
;
using
Microsoft.CodeAnalysis.CodeRefactorings
;
using
Microsoft.CodeAnalysis.CodeStyle
;
using
Microsoft.CodeAnalysis.CSharp
;
using
Microsoft.CodeAnalysis.CSharp.CodeStyle
;
using
Microsoft.CodeAnalysis.CSharp.Test.Utilities
;
using
Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.CodeRefactorings
;
using
Microsoft.CodeAnalysis.GenerateEqualsAndGetHashCodeFromMembers
;
using
Microsoft.CodeAnalysis.Options
;
using
Microsoft.CodeAnalysis.PickMembers
;
using
Microsoft.CodeAnalysis.Test.Utilities
;
using
Roslyn.Test.Utilities
;
...
...
@@ -22,9 +25,28 @@ public class GenerateEqualsAndGetHashCodeFromMembersTests : AbstractCSharpCodeAc
private
static
readonly
TestParameters
CSharp6
=
new
TestParameters
(
parseOptions
:
TestOptions
.
Regular
.
WithLanguageVersion
(
LanguageVersion
.
CSharp6
));
private
TestParameters
CSharp6Implicit
=>
CSharp6
.
WithOptions
(
ImplicitTypingEverywhere
());
private
TestParameters
CSharp6Explicit
=>
CSharp6
.
WithOptions
(
ExplicitTypingEverywhere
());
protected
override
CodeRefactoringProvider
CreateCodeRefactoringProvider
(
Workspace
workspace
,
TestParameters
parameters
)
=>
new
GenerateEqualsAndGetHashCodeFromMembersCodeRefactoringProvider
((
IPickMembersService
)
parameters
.
fixProviderData
);
private
readonly
CodeStyleOption
<
bool
>
onWithInfo
=
new
CodeStyleOption
<
bool
>(
true
,
NotificationOption
.
Suggestion
);
private
readonly
CodeStyleOption
<
bool
>
offWithInfo
=
new
CodeStyleOption
<
bool
>(
false
,
NotificationOption
.
Suggestion
);
private
IDictionary
<
OptionKey
,
object
>
ExplicitTypingEverywhere
()
=>
OptionsSet
(
SingleOption
(
CSharpCodeStyleOptions
.
VarElsewhere
,
offWithInfo
),
SingleOption
(
CSharpCodeStyleOptions
.
VarWhenTypeIsApparent
,
offWithInfo
),
SingleOption
(
CSharpCodeStyleOptions
.
VarForBuiltInTypes
,
offWithInfo
));
private
IDictionary
<
OptionKey
,
object
>
ImplicitTypingEverywhere
()
=>
OptionsSet
(
SingleOption
(
CSharpCodeStyleOptions
.
VarElsewhere
,
onWithInfo
),
SingleOption
(
CSharpCodeStyleOptions
.
VarWhenTypeIsApparent
,
onWithInfo
),
SingleOption
(
CSharpCodeStyleOptions
.
VarForBuiltInTypes
,
onWithInfo
));
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsGenerateEqualsAndGetHashCode
)]
public
async
Task
TestEqualsSingleField
()
{
...
...
@@ -48,7 +70,33 @@ public override bool Equals(object obj)
a == program.a;
}
}"
,
parameters
:
CSharp6
);
parameters
:
CSharp6Implicit
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsGenerateEqualsAndGetHashCode
)]
public
async
Task
TestEqualsSingleField_PreferExplicitType
()
{
await
TestInRegularAndScript1Async
(
@"using System.Collections.Generic;
class Program
{
[|int a;|]
}"
,
@"using System.Collections.Generic;
class Program
{
int a;
public override bool Equals(object obj)
{
Program program = obj as Program;
return program != null &&
a == program.a;
}
}"
,
parameters
:
CSharp6Explicit
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsGenerateEqualsAndGetHashCode
)]
...
...
@@ -82,13 +130,13 @@ public override bool Equals(object obj)
EqualityComparer<S>.Default.Equals(a, program.a);
}
}"
,
parameters
:
CSharp6
);
parameters
:
CSharp6
Implicit
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsGenerateEqualsAndGetHashCode
)]
public
async
Task
TestNullableReferenceIEquatable
()
{
await
TestInRegularAndScript
1
Async
(
await
TestInRegularAndScriptAsync
(
@"#nullable enable
using System;
...
...
@@ -121,7 +169,7 @@ public override int GetHashCode()
{
return -1757793268 + EqualityComparer<S?>.Default.GetHashCode(a);
}
}"
,
index
:
1
);
}"
,
index
:
1
,
options
:
ImplicitTypingEverywhere
()
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsGenerateEqualsAndGetHashCode
)]
...
...
@@ -155,7 +203,7 @@ public override bool Equals(object obj)
a.Equals(program.a);
}
}"
,
parameters
:
CSharp6
);
parameters
:
CSharp6
Implicit
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsGenerateEqualsAndGetHashCode
)]
...
...
@@ -181,7 +229,7 @@ public override bool Equals(object obj)
a == name.a;
}
}"
,
parameters
:
CSharp6
);
parameters
:
CSharp6
Implicit
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsGenerateEqualsAndGetHashCode
)]
...
...
@@ -207,7 +255,7 @@ public override bool Equals(object obj)
a == @long.a;
}
}"
,
parameters
:
CSharp6
);
parameters
:
CSharp6
Implicit
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsGenerateEqualsAndGetHashCode
)]
...
...
@@ -238,7 +286,7 @@ public override bool Equals(object obj)
B == name.B;
}
}"
,
parameters
:
CSharp6
);
parameters
:
CSharp6
Implicit
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsGenerateEqualsAndGetHashCode
)]
...
...
@@ -268,7 +316,7 @@ public override bool Equals(object obj)
i == program.i;
}
}"
,
parameters
:
CSharp6
);
parameters
:
CSharp6
Implicit
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsGenerateEqualsAndGetHashCode
)]
...
...
@@ -315,7 +363,7 @@ public override bool Equals(object obj)
}
}"
,
index
:
0
,
parameters
:
CSharp6
);
parameters
:
CSharp6
Implicit
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsGenerateEqualsAndGetHashCode
)]
...
...
@@ -369,7 +417,7 @@ public override bool Equals(object obj)
S == program.S;
}
}"
,
parameters
:
CSharp6
);
parameters
:
CSharp6
Implicit
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsGenerateEqualsAndGetHashCode
)]
...
...
@@ -404,7 +452,7 @@ public override bool Equals(object obj)
S == name.S;
}
}"
,
parameters
:
CSharp6
);
parameters
:
CSharp6
Implicit
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsGenerateEqualsAndGetHashCode
)]
...
...
@@ -434,7 +482,7 @@ public override bool Equals(object obj)
"
;
await
TestInRegularAndScript1Async
(
code
,
expected
,
parameters
:
CSharp6
);
parameters
:
CSharp6
Implicit
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsGenerateEqualsAndGetHashCode
)]
...
...
@@ -490,7 +538,7 @@ public override int GetHashCode()
}
}"
,
index
:
1
,
parameters
:
CSharp6
);
parameters
:
CSharp6
Implicit
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsGenerateEqualsAndGetHashCode
)]
...
...
@@ -522,7 +570,7 @@ public override int GetHashCode()
}
}"
,
index
:
1
,
parameters
:
CSharp6
);
parameters
:
CSharp6
Implicit
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsGenerateEqualsAndGetHashCode
)]
...
...
@@ -565,7 +613,7 @@ public override int GetHashCode()
}
}"
,
index
:
1
,
parameters
:
CSharp6
);
parameters
:
CSharp6
Implicit
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsGenerateEqualsAndGetHashCode
)]
...
...
@@ -606,7 +654,7 @@ public override int GetHashCode()
}"
,
chosenSymbols
:
new
string
[]
{
},
index
:
1
,
parameters
:
CSharp6
);
parameters
:
CSharp6
Implicit
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsGenerateEqualsAndGetHashCode
)]
...
...
@@ -627,7 +675,7 @@ class Program
public override bool Equals(object obj)
{
var
program = obj as Program;
Program
program = obj as Program;
return program != null &&
i == program.i;
}
...
...
@@ -669,7 +717,7 @@ public override int GetHashCode()
}
}"
,
index
:
1
,
parameters
:
CSharp6
);
parameters
:
CSharp6
Implicit
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsGenerateEqualsAndGetHashCode
)]
...
...
@@ -701,7 +749,7 @@ public override int GetHashCode()
}
}"
,
index
:
1
,
parameters
:
CSharp6
);
parameters
:
CSharp6
Implicit
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsGenerateEqualsAndGetHashCode
)]
...
...
@@ -741,7 +789,7 @@ public override int GetHashCode()
}
}"
,
index
:
1
,
parameters
:
CSharp6
);
parameters
:
CSharp6
Implicit
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsGenerateEqualsAndGetHashCode
)]
...
...
@@ -827,7 +875,7 @@ public override bool Equals(object obj)
}
}"
,
index
:
0
,
parameters
:
CSharp6
);
parameters
:
CSharp6
Implicit
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsGenerateEqualsAndGetHashCode
)]
...
...
@@ -853,7 +901,7 @@ public override bool Equals(object obj)
a.Equals(c.a);
}
}"
,
parameters
:
CSharp6
);
parameters
:
CSharp6
Implicit
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsGenerateEqualsAndGetHashCode
)]
...
...
@@ -879,7 +927,7 @@ public override bool Equals(object obj)
a.Equals(c.a);
}
}"
,
parameters
:
CSharp6
);
parameters
:
CSharp6
Implicit
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsGenerateEqualsAndGetHashCode
)]
...
...
@@ -911,7 +959,7 @@ public override int GetHashCode()
}
}"
,
index
:
1
,
parameters
:
CSharp6
);
parameters
:
CSharp6
Implicit
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsGenerateEqualsAndGetHashCode
)]
...
...
@@ -943,7 +991,7 @@ public override int GetHashCode()
}
}"
,
index
:
1
,
parameters
:
CSharp6
);
parameters
:
CSharp6
Implicit
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsGenerateEqualsAndGetHashCode
)]
...
...
@@ -983,7 +1031,7 @@ struct Bar
{
}"
,
index
:
1
,
parameters
:
CSharp6
);
parameters
:
CSharp6
Implicit
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsGenerateEqualsAndGetHashCode
)]
...
...
@@ -1025,7 +1073,7 @@ struct Bar
public override int GetHashCode() => 0;
}"
,
index
:
1
,
parameters
:
CSharp6
);
parameters
:
CSharp6
Implicit
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsGenerateEqualsAndGetHashCode
)]
...
...
@@ -1065,7 +1113,7 @@ struct Bar
{
}"
,
index
:
1
,
parameters
:
CSharp6
);
parameters
:
CSharp6
Implicit
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsGenerateEqualsAndGetHashCode
)]
...
...
@@ -1097,7 +1145,7 @@ public override int GetHashCode()
}
}"
,
index
:
1
,
parameters
:
CSharp6
);
parameters
:
CSharp6
Implicit
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsGenerateEqualsAndGetHashCode
)]
...
...
@@ -1129,7 +1177,7 @@ public override int GetHashCode()
}
}"
,
index
:
1
,
parameters
:
CSharp6
);
parameters
:
CSharp6
Implicit
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsGenerateEqualsAndGetHashCode
)]
...
...
@@ -1169,7 +1217,7 @@ enum Bar
{
}"
,
index
:
1
,
parameters
:
CSharp6
);
parameters
:
CSharp6
Implicit
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsGenerateEqualsAndGetHashCode
)]
...
...
@@ -1201,7 +1249,7 @@ public override int GetHashCode()
}
}"
,
index
:
1
,
parameters
:
CSharp6
);
parameters
:
CSharp6
Implicit
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsGenerateEqualsAndGetHashCode
)]
...
...
@@ -1232,7 +1280,7 @@ public override bool Equals(object obj)
}
}"
,
chosenSymbols
:
new
[]
{
"a"
,
"b"
},
parameters
:
CSharp6
);
parameters
:
CSharp6
Implicit
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsGenerateEqualsAndGetHashCode
)]
...
...
@@ -1265,7 +1313,7 @@ public override bool Equals(object obj)
}
}"
,
chosenSymbols
:
new
[]
{
"c"
,
"b"
},
parameters
:
CSharp6
);
parameters
:
CSharp6
Implicit
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsGenerateEqualsAndGetHashCode
)]
...
...
@@ -1296,7 +1344,7 @@ public override bool Equals(object obj)
}
}"
,
chosenSymbols
:
new
string
[]
{
},
parameters
:
CSharp6
);
parameters
:
CSharp6
Implicit
);
}
[
WorkItem
(
17643
,
"https://github.com/dotnet/roslyn/issues/17643"
)]
...
...
@@ -1323,7 +1371,7 @@ public override bool Equals(object obj)
}
}"
,
chosenSymbols
:
null
,
parameters
:
CSharp6
);
parameters
:
CSharp6
Implicit
);
}
[
WorkItem
(
25690
,
"https://github.com/dotnet/roslyn/issues/25690"
)]
...
...
@@ -1418,7 +1466,7 @@ public override bool Equals(object obj)
}"
,
chosenSymbols
:
null
,
optionsCallback
:
options
=>
EnableOption
(
options
,
GenerateOperatorsId
),
parameters
:
CSharp6
);
parameters
:
CSharp6
Implicit
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsGenerateEqualsAndGetHashCode
)]
...
...
@@ -1442,7 +1490,7 @@ class Program
public override bool Equals(object obj)
{
var
program = obj as Program;
Program
program = obj as Program;
return program != null &&
s == program.s;
}
...
...
@@ -1489,7 +1537,7 @@ public override bool Equals(object obj)
}"
,
chosenSymbols
:
null
,
optionsCallback
:
options
=>
Assert
.
Null
(
options
.
FirstOrDefault
(
i
=>
i
.
Id
==
GenerateOperatorsId
)),
parameters
:
CSharp6
);
parameters
:
CSharp6
Implicit
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsGenerateEqualsAndGetHashCode
)]
...
...
@@ -1534,7 +1582,7 @@ public override bool Equals(object obj)
}"
,
chosenSymbols
:
null
,
optionsCallback
:
options
=>
EnableOption
(
options
,
GenerateOperatorsId
),
parameters
:
CSharp6
);
parameters
:
CSharp6
Implicit
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsGenerateEqualsAndGetHashCode
)]
...
...
@@ -1582,7 +1630,7 @@ enum Bar
{
}"
,
index
:
0
,
parameters
:
CSharp6
);
parameters
:
CSharp6
Implicit
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsGenerateEqualsAndGetHashCode
)]
...
...
@@ -1646,7 +1694,7 @@ struct Bar : IEquatable<Bar>
public static bool operator !=(Bar left, Bar right) => !(left == right);
}"
,
index
:
0
,
parameters
:
CSharp6
);
parameters
:
CSharp6
Implicit
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsGenerateEqualsAndGetHashCode
)]
...
...
@@ -1681,7 +1729,7 @@ public bool Equals(Program other)
}"
,
chosenSymbols
:
null
,
optionsCallback
:
options
=>
EnableOption
(
options
,
ImplementIEquatableId
),
parameters
:
CSharp6
);
parameters
:
CSharp6
Implicit
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsGenerateEqualsAndGetHashCode
)]
...
...
@@ -1830,7 +1878,7 @@ public bool Equals(Program other)
}"
,
chosenSymbols
:
null
,
optionsCallback
:
options
=>
EnableOption
(
options
,
ImplementIEquatableId
),
parameters
:
CSharp6
);
parameters
:
CSharp6
Implicit
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsGenerateEqualsAndGetHashCode
)]
...
...
@@ -1976,7 +2024,7 @@ public override bool Equals(object obj)
}"
,
chosenSymbols
:
null
,
optionsCallback
:
options
=>
Assert
.
Null
(
options
.
FirstOrDefault
(
i
=>
i
.
Id
==
ImplementIEquatableId
)),
parameters
:
CSharp6
);
parameters
:
CSharp6
Implicit
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsGenerateEqualsAndGetHashCode
)]
...
...
@@ -2006,7 +2054,7 @@ public class Class1
public override global::System.Boolean Equals(global::System.Object obj)
{
var
@class = obj as Class1;
Class1
@class = obj as Class1;
return @class != null;
}
...
...
@@ -2046,7 +2094,7 @@ class Program
public override bool Equals(object obj)
{
var
program = obj as Program;
Program
program = obj as Program;
return program != null &&
i == program.i &&
S == program.S;
...
...
@@ -2056,7 +2104,7 @@ public override int GetHashCode()
{
unchecked
{
var
hashCode = -538000506;
int
hashCode = -538000506;
hashCode = hashCode * -1521134295 + i.GetHashCode();
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(S);
return hashCode;
...
...
@@ -2103,7 +2151,7 @@ public override int GetHashCode()
}
}"
,
index
:
1
,
parameters
:
CSharp6
);
parameters
:
CSharp6
Implicit
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsGenerateEqualsAndGetHashCode
)]
...
...
@@ -2140,7 +2188,7 @@ public override int GetHashCode()
}
}"
,
index
:
1
,
parameters
:
CSharp6
);
parameters
:
CSharp6
Implicit
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsGenerateEqualsAndGetHashCode
)]
...
...
@@ -2184,7 +2232,7 @@ public override int GetHashCode()
}
}"
,
index
:
1
,
parameters
:
CSharp6
);
parameters
:
CSharp6
Implicit
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsGenerateEqualsAndGetHashCode
)]
...
...
@@ -2239,7 +2287,62 @@ public override int GetHashCode()
}
}"
,
index
:
1
,
parameters
:
CSharp6
);
parameters
:
CSharp6Implicit
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsGenerateEqualsAndGetHashCode
)]
public
async
Task
TestGetHashCodeSystemHashCodeNineMembers_Explicit
()
{
await
TestInRegularAndScript1Async
(
@"using System.Collections.Generic;
namespace System { public struct HashCode { } }
struct S
{
[|int j, k, l, m, n, o, p, q, r;|]
}"
,
@"using System;
using System.Collections.Generic;
namespace System { public struct HashCode { } }
struct S
{
int j, k, l, m, n, o, p, q, r;
public override bool Equals(object obj)
{
if (!(obj is S))
{
return false;
}
S s = (S)obj;
return j == s.j &&
k == s.k &&
l == s.l &&
m == s.m &&
n == s.n &&
o == s.o &&
p == s.p &&
q == s.q &&
r == s.r;
}
public override int GetHashCode()
{
HashCode hash = new HashCode();
hash.Add(j);
hash.Add(k);
hash.Add(l);
hash.Add(m);
hash.Add(n);
hash.Add(o);
hash.Add(p);
hash.Add(q);
hash.Add(r);
return hash.ToHashCode();
}
}"
,
index
:
1
,
parameters
:
CSharp6Explicit
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsGenerateEqualsAndGetHashCode
)]
...
...
src/Workspaces/CSharp/Portable/CodeGeneration/CSharpSyntaxGenerator.cs
浏览文件 @
1df59ddc
...
...
@@ -3251,6 +3251,12 @@ public override SyntaxNode ThrowExpression(SyntaxNode expression)
internal
override
bool
SupportsThrowExpression
()
=>
true
;
/// <summary>
/// C# always requires a type to be present with a local declaration. (Even if that type is
/// 'var').
/// </summary>
internal
override
bool
RequiresLocalDeclarationType
()
=>
true
;
public
override
SyntaxNode
IfStatement
(
SyntaxNode
condition
,
IEnumerable
<
SyntaxNode
>
trueStatements
,
IEnumerable
<
SyntaxNode
>
falseStatements
=
null
)
{
if
(
falseStatements
==
null
)
...
...
src/Workspaces/Core/Portable/Editing/SyntaxGenerator.cs
浏览文件 @
1df59ddc
...
...
@@ -1443,6 +1443,14 @@ protected static SyntaxList<TNode> RemoveRange<TNode>(SyntaxList<TNode> list, in
/// </summary>
internal
abstract
bool
SupportsThrowExpression
();
/// <summary>
/// <see langword="true"/> if the language requires a <see cref="TypeExpression(ITypeSymbol)"/>
/// (including <see langword="var"/>) to be stated when making a
/// <see cref="LocalDeclarationStatement(ITypeSymbol, string, SyntaxNode, bool)"/>.
/// <see langword="false"/> if the language allows the type node to be entirely elided.
/// </summary>
internal
abstract
bool
RequiresLocalDeclarationType
();
/// <summary>
/// Creates a statement that declares a single local variable.
/// </summary>
...
...
src/Workspaces/Core/Portable/Shared/Extensions/SyntaxGeneratorExtensions_CreateEqualsMethod.cs
浏览文件 @
1df59ddc
...
...
@@ -149,7 +149,8 @@ public static IMethodSymbol CreateEqualsMethod(this Compilation compilation, Imm
//
// var myType = (MyType)obj;
var
localDeclaration
=
factory
.
LocalDeclarationStatement
(
localName
,
factory
.
CastExpression
(
containingType
,
objNameExpression
));
var
localDeclaration
=
factory
.
SimpleLocalDeclarationStatement
(
containingType
,
localName
,
factory
.
CastExpression
(
containingType
,
objNameExpression
));
statements
.
Add
(
ifStatement
);
statements
.
Add
(
localDeclaration
);
...
...
@@ -160,7 +161,8 @@ public static IMethodSymbol CreateEqualsMethod(this Compilation compilation, Imm
//
// var myType = obj as MyType;
var
localDeclaration
=
factory
.
LocalDeclarationStatement
(
localName
,
factory
.
TryCastExpression
(
objNameExpression
,
containingType
));
var
localDeclaration
=
factory
.
SimpleLocalDeclarationStatement
(
containingType
,
localName
,
factory
.
TryCastExpression
(
objNameExpression
,
containingType
));
statements
.
Add
(
localDeclaration
);
...
...
src/Workspaces/Core/Portable/Shared/Extensions/SyntaxGeneratorExtensions_CreateGetHashCodeMethod.cs
浏览文件 @
1df59ddc
...
...
@@ -2,6 +2,7 @@
#
nullable
enable
using
System
;
using
System.Collections.Immutable
;
using
System.Linq
;
using
Microsoft.CodeAnalysis.Editing
;
...
...
@@ -51,8 +52,8 @@ internal static partial class SyntaxGeneratorExtensions
const
string
hashName
=
"hash"
;
var
statements
=
ArrayBuilder
<
SyntaxNode
>.
GetInstance
();
statements
.
Add
(
factory
.
LocalDeclarationStatement
(
hashName
,
factory
.
ObjectCreationExpression
(
hashCodeType
)));
statements
.
Add
(
factory
.
SimpleLocalDeclarationStatement
(
hashCodeType
,
hashName
,
factory
.
ObjectCreationExpression
(
hashCodeType
)));
var
localReference
=
factory
.
IdentifierName
(
hashName
);
foreach
(
var
member
in
memberReferences
)
...
...
@@ -130,7 +131,7 @@ internal static partial class SyntaxGeneratorExtensions
const
string
HashCodeName
=
"hashCode"
;
statements
.
Add
(!
useInt64
?
factory
.
LocalDeclarationStatement
(
HashCodeName
,
CreateLiteralExpression
(
factory
,
initHash
))
?
factory
.
SimpleLocalDeclarationStatement
(
compilation
.
GetSpecialType
(
SpecialType
.
System_Int32
),
HashCodeName
,
CreateLiteralExpression
(
factory
,
initHash
))
:
factory
.
LocalDeclarationStatement
(
compilation
.
GetSpecialType
(
SpecialType
.
System_Int64
),
HashCodeName
,
CreateLiteralExpression
(
factory
,
initHash
)));
var
hashCodeNameExpression
=
factory
.
IdentifierName
(
HashCodeName
);
...
...
@@ -166,6 +167,21 @@ internal static partial class SyntaxGeneratorExtensions
return
statements
.
ToImmutableAndFree
();
}
/// <summary>
/// In VB it's more idiomatic to write things like <c>Dim t = TryCast(obj, SomeType)</c>
/// instead of <c>Dim t As SomeType = TryCast(obj, SomeType)</c>, so we just elide the type
/// from the decl. For C# we don't want to do this though. We want to always include the
/// type and let the simplifier decide if it should be <c>var</c> or not.
/// </summary>
private
static
SyntaxNode
SimpleLocalDeclarationStatement
(
this
SyntaxGenerator
generator
,
INamedTypeSymbol
namedTypeSymbol
,
string
name
,
SyntaxNode
initializer
)
{
return
generator
.
RequiresLocalDeclarationType
()
?
generator
.
LocalDeclarationStatement
(
namedTypeSymbol
,
name
,
initializer
)
:
generator
.
LocalDeclarationStatement
(
name
,
initializer
);
}
private
static
SyntaxNode
CreateLiteralExpression
(
SyntaxGenerator
factory
,
int
value
)
=>
value
<
0
?
factory
.
NegateExpression
(
factory
.
LiteralExpression
(-
value
))
...
...
@@ -203,7 +219,7 @@ private static SyntaxNode CreateLiteralExpression(SyntaxGenerator factory, int v
{
var
getHashCodeNameExpression
=
factory
.
IdentifierName
(
GetHashCodeName
);
var
thisSymbol
=
factory
.
MemberAccessExpression
(
factory
.
ThisExpression
(),
factory
.
IdentifierName
(
member
.
Name
)).
WithAdditionalAnnotations
(
Simplification
.
Simplifier
.
Annotation
);
factory
.
IdentifierName
(
member
.
Name
)).
WithAdditionalAnnotations
<
SyntaxNode
>
(
Simplification
.
Simplifier
.
Annotation
);
// Caller only wanted the reference to the member, nothing else added.
if
(
justMemberReference
)
...
...
src/Workspaces/VisualBasic/Portable/CodeGeneration/VisualBasicSyntaxGenerator.vb
浏览文件 @
1df59ddc
...
...
@@ -4196,6 +4196,12 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeGeneration
Return
False
End
Function
Friend
Overrides
Function
RequiresLocalDeclarationType
()
As
Boolean
' VB supports `dim x = ...` as well as `dim x as Y = ...`. The local declaration type
' is not required.
Return
False
End
Function
Friend
Overrides
Function
IsPatternExpression
(
expression
As
SyntaxNode
,
pattern
As
SyntaxNode
)
As
SyntaxNode
Throw
New
NotImplementedException
()
End
Function
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录