Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
0bf0cedf
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,发现更多精彩内容 >>
提交
0bf0cedf
编写于
11月 04, 2016
作者:
C
CyrusNajmabadi
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Don't suggest use-object-initializer when it would change code semantics
上级
68e04689
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
136 addition
and
5 deletion
+136
-5
src/EditorFeatures/CSharpTest/UseObjectInitializer/UseObjectInitializerTests.cs
...arpTest/UseObjectInitializer/UseObjectInitializerTests.cs
+95
-0
src/Features/Core/Portable/Diagnostics/Analyzers/NamingStyles/Serialization/SerializableNamingRule.cs
...zers/NamingStyles/Serialization/SerializableNamingRule.cs
+0
-3
src/Features/Core/Portable/UseObjectInitializer/AbstractUseObjectInitializerDiagnosticAnalyzer.cs
...ializer/AbstractUseObjectInitializerDiagnosticAnalyzer.cs
+41
-2
未找到文件。
src/EditorFeatures/CSharpTest/UseObjectInitializer/UseObjectInitializerTests.cs
浏览文件 @
0bf0cedf
...
...
@@ -49,6 +49,101 @@ void M()
}"
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsUseObjectInitializer
)]
public
async
Task
TestDoNotUpdateAssignmentThatReferencesInitializedValue1Async
()
{
await
TestAsync
(
@"class C
{
int i;
void M()
{
var c = [||]new C();
c.i = 1;
c.i = c.i + 1;
}
}"
,
@"class C
{
int i;
void M()
{
var c = new C()
{
i = 1
};
c.i = c.i + 1;
}
}"
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsUseObjectInitializer
)]
public
async
Task
TestDoNotUpdateAssignmentThatReferencesInitializedValue2Async
()
{
await
TestMissingAsync
(
@"class C
{
int i;
void M()
{
var c = [||]new C();
c.i = c.i + 1;
}
}"
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsUseObjectInitializer
)]
public
async
Task
TestDoNotUpdateAssignmentThatReferencesInitializedValue3Async
()
{
await
TestAsync
(
@"class C
{
int i;
void M()
{
C c;
c = [||]new C();
c.i = 1;
c.i = c.i + 1;
}
}"
,
@"class C
{
int i;
void M()
{
C c;
c = new C()
{
i = 1
};
c.i = c.i + 1;
}
}"
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsUseObjectInitializer
)]
public
async
Task
TestDoNotUpdateAssignmentThatReferencesInitializedValue4Async
()
{
await
TestMissingAsync
(
@"class C
{
int i;
void M()
{
C c;
c = [||]new C();
c.i = c.i + 1;
}
}"
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsUseObjectInitializer
)]
public
async
Task
TestOnAssignmentExpression
()
{
...
...
src/Features/Core/Portable/Diagnostics/Analyzers/NamingStyles/Serialization/SerializableNamingRule.cs
浏览文件 @
0bf0cedf
// 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
;
using
System.Collections.Generic
;
using
System.Collections.Immutable
;
using
System.Linq
;
using
System.Xml.Linq
;
namespace
Microsoft.CodeAnalysis.Diagnostics.Analyzers.NamingStyles
...
...
src/Features/Core/Portable/UseObjectInitializer/AbstractUseObjectInitializerDiagnosticAnalyzer.cs
浏览文件 @
0bf0cedf
...
...
@@ -7,6 +7,8 @@
using
Microsoft.CodeAnalysis.LanguageServices
;
using
Microsoft.CodeAnalysis.Text
;
using
Microsoft.CodeAnalysis.Options
;
using
System
;
using
System.Linq
;
namespace
Microsoft.CodeAnalysis.UseObjectInitializer
{
...
...
@@ -236,8 +238,8 @@ internal struct Analyzer<
break
;
}
SyntaxNode
left
,
right
;
_syntaxFacts
.
GetPartsOfAssignmentStatement
(
statement
,
out
left
,
out
right
);
_syntaxFacts
.
GetPartsOfAssignmentStatement
(
statement
,
out
var
left
,
out
var
right
);
var
rightExpression
=
right
as
TExpressionSyntax
;
var
leftMemberAccess
=
left
as
TMemberAccessExpressionSyntax
;
...
...
@@ -252,6 +254,27 @@ internal struct Analyzer<
break
;
}
// Don't offer this fix if the value we're initializing is itself referenced
// on the RHS of the assignment. For example:
//
// var v = new X();
// v.Prop = v.Prop.WithSomething();
//
// Or with
//
// v = new X();
// v.Prop = v.Prop.WithSomething();
//
// In the first case, 'v' is being initialized, and so will not be available
// in the object initializer we create.
//
// In the second case we'd change semantics because we'd access the old value
// before the new value got written.
if
(
ExpressionContainsValuePattern
(
rightExpression
))
{
break
;
}
// found a match!
seenNames
=
seenNames
??
new
HashSet
<
string
>();
...
...
@@ -271,6 +294,22 @@ internal struct Analyzer<
return
matches
.
ToImmutableAndFree
();
}
private
bool
ExpressionContainsValuePattern
(
TExpressionSyntax
expression
)
{
foreach
(
var
subExpression
in
expression
.
DescendantNodesAndSelf
().
OfType
<
TExpressionSyntax
>())
{
if
(!
_syntaxFacts
.
IsNameOfMemberAccessExpression
(
subExpression
))
{
if
(
ValuePatternMatches
(
subExpression
))
{
return
true
;
}
}
}
return
false
;
}
private
bool
ValuePatternMatches
(
TExpressionSyntax
expression
)
{
if
(
_valuePattern
.
IsToken
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录