Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
1fd082e1
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,发现更多精彩内容 >>
提交
1fd082e1
编写于
11月 07, 2016
作者:
C
CyrusNajmabadi
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Do not offer to use an explicit type for 'var' when it's an anonymous type.
上级
bf475922
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
96 addition
and
11 deletion
+96
-11
src/EditorFeatures/CSharpTest/Diagnostics/UseImplicitOrExplicitType/UseExplicitTypeTests.cs
...nostics/UseImplicitOrExplicitType/UseExplicitTypeTests.cs
+57
-0
src/Features/CSharp/Portable/Diagnostics/Analyzers/CSharpTypeStyleDiagnosticAnalyzerBase.State.cs
.../Analyzers/CSharpTypeStyleDiagnosticAnalyzerBase.State.cs
+1
-1
src/Features/CSharp/Portable/Diagnostics/Analyzers/CSharpTypeStyleDiagnosticAnalyzerBase.cs
...ostics/Analyzers/CSharpTypeStyleDiagnosticAnalyzerBase.cs
+5
-5
src/Features/CSharp/Portable/Diagnostics/Analyzers/CSharpUseExplicitTypeDiagnosticAnalyzer.cs
...tics/Analyzers/CSharpUseExplicitTypeDiagnosticAnalyzer.cs
+23
-3
src/Features/CSharp/Portable/Diagnostics/Analyzers/CSharpUseImplicitTypeDiagnosticAnalyzer.cs
...tics/Analyzers/CSharpUseImplicitTypeDiagnosticAnalyzer.cs
+10
-2
未找到文件。
src/EditorFeatures/CSharpTest/Diagnostics/UseImplicitOrExplicitType/UseExplicitTypeTests.cs
浏览文件 @
1fd082e1
...
...
@@ -238,6 +238,63 @@ void Method()
}"
,
options
:
ExplicitTypeEverywhere
());
}
[
WpfFact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsUseExplicitType
)]
public
async
Task
NotOnForEachVarWithAnonymousType
()
{
await
TestMissingAsync
(
@"using System;
using System.Linq;
class Program
{
void Method()
{
var values = Enumerable.Range(1, 5).Select(i => new { Value = i });
foreach ([|var|] value in values)
{
Console.WriteLine(value.Value);
}
}
}"
,
options
:
ExplicitTypeEverywhere
());
}
[
WpfFact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsUseExplicitType
)]
public
async
Task
OnForEachVarWithExplicitType
()
{
await
TestAsync
(
@"using System;
using System.Linq;
class Program
{
void Method()
{
var values = Enumerable.Range(1, 5);
foreach ([|var|] value in values)
{
Console.WriteLine(value.Value);
}
}
}"
,
@"using System;
using System.Linq;
class Program
{
void Method()
{
var values = Enumerable.Range(1, 5);
foreach (int value in values)
{
Console.WriteLine(value.Value);
}
}
}"
,
options
:
ExplicitTypeEverywhere
());
}
[
WpfFact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsUseExplicitType
)]
public
async
Task
NotOnAnonymousType
()
{
...
...
src/Features/CSharp/Portable/Diagnostics/Analyzers/CSharpTypeStyleDiagnosticAnalyzerBase.State.cs
浏览文件 @
1fd082e1
...
...
@@ -75,7 +75,7 @@ private void Initialize(SyntaxNode declaration, SemanticModel semanticModel, Opt
private
bool
IsTypeApparentInDeclaration
(
VariableDeclarationSyntax
variableDeclaration
,
SemanticModel
semanticModel
,
TypeStylePreference
stylePreferences
,
CancellationToken
cancellationToken
)
{
var
initializer
=
variableDeclaration
.
Variables
.
Single
().
Initializer
;
var
initializerExpression
=
GetInitializerExpression
(
initializer
);
var
initializerExpression
=
GetInitializerExpression
(
initializer
.
Value
);
var
declaredTypeSymbol
=
semanticModel
.
GetTypeInfo
(
variableDeclaration
.
Type
,
cancellationToken
).
Type
;
return
TypeStyleHelper
.
IsTypeApparentInAssignmentExpression
(
stylePreferences
,
initializerExpression
,
semanticModel
,
cancellationToken
,
declaredTypeSymbol
);
}
...
...
src/Features/CSharp/Portable/Diagnostics/Analyzers/CSharpTypeStyleDiagnosticAnalyzerBase.cs
浏览文件 @
1fd082e1
...
...
@@ -58,12 +58,12 @@ public override void Initialize(AnalysisContext context)
protected
abstract
bool
IsStylePreferred
(
SemanticModel
semanticModel
,
OptionSet
optionSet
,
State
state
,
CancellationToken
cancellationToken
);
protected
abstract
bool
TryAnalyzeVariableDeclaration
(
TypeSyntax
typeName
,
SemanticModel
semanticModel
,
OptionSet
optionSet
,
CancellationToken
cancellationToken
,
out
TextSpan
issueSpan
);
protected
abstract
bool
AssignmentSupportsStylePreference
(
SyntaxToken
identifier
,
TypeSyntax
typeName
,
E
qualsValueClause
Syntax
initializer
,
SemanticModel
semanticModel
,
OptionSet
optionSet
,
CancellationToken
cancellationToken
);
protected
abstract
bool
AssignmentSupportsStylePreference
(
SyntaxToken
identifier
,
TypeSyntax
typeName
,
E
xpression
Syntax
initializer
,
SemanticModel
semanticModel
,
OptionSet
optionSet
,
CancellationToken
cancellationToken
);
protected
static
ExpressionSyntax
GetInitializerExpression
(
E
qualsValueClause
Syntax
initializer
)
=>
initializer
.
Value
is
CheckedExpressionSyntax
?
((
CheckedExpressionSyntax
)
initializer
.
Value
).
Expression
.
WalkDownParentheses
()
:
initializer
.
Value
.
WalkDownParentheses
();
protected
static
ExpressionSyntax
GetInitializerExpression
(
E
xpression
Syntax
initializer
)
=>
initializer
is
CheckedExpressionSyntax
?
((
CheckedExpressionSyntax
)
initializer
).
Expression
.
WalkDownParentheses
()
:
initializer
.
WalkDownParentheses
();
private
void
HandleVariableDeclaration
(
SyntaxNodeAnalysisContext
context
)
{
...
...
src/Features/CSharp/Portable/Diagnostics/Analyzers/CSharpUseExplicitTypeDiagnosticAnalyzer.cs
浏览文件 @
1fd082e1
...
...
@@ -64,12 +64,26 @@ protected override bool TryAnalyzeVariableDeclaration(TypeSyntax typeName, Seman
return
false
;
}
var
isForeachDecl
=
typeName
.
Parent
.
IsKind
(
SyntaxKind
.
ForEachStatement
);
if
(
typeName
.
Parent
.
IsKind
(
SyntaxKind
.
VariableDeclaration
)
&&
typeName
.
Parent
.
Parent
.
IsKind
(
SyntaxKind
.
LocalDeclarationStatement
,
SyntaxKind
.
ForStatement
,
SyntaxKind
.
UsingStatement
))
{
// check assignment for variable declarations.
var
variable
=
((
VariableDeclarationSyntax
)
typeName
.
Parent
).
Variables
.
First
();
if
(!
AssignmentSupportsStylePreference
(
variable
.
Identifier
,
typeName
,
variable
.
Initializer
,
semanticModel
,
optionSet
,
cancellationToken
))
if
(!
AssignmentSupportsStylePreference
(
variable
.
Identifier
,
typeName
,
variable
.
Initializer
.
Value
,
semanticModel
,
optionSet
,
cancellationToken
))
{
return
false
;
}
}
else
if
(
typeName
.
Parent
.
IsKind
(
SyntaxKind
.
ForEachStatement
))
{
var
foreachStatement
=
(
ForEachStatementSyntax
)
typeName
.
Parent
;
if
(!
AssignmentSupportsStylePreference
(
foreachStatement
.
Identifier
,
typeName
,
foreachStatement
.
Expression
,
semanticModel
,
optionSet
,
cancellationToken
))
{
return
false
;
}
...
...
@@ -86,7 +100,13 @@ protected override bool TryAnalyzeVariableDeclaration(TypeSyntax typeName, Seman
/// false, if explicit typing cannot be used.
/// true, otherwise.
/// </returns>
protected
override
bool
AssignmentSupportsStylePreference
(
SyntaxToken
identifier
,
TypeSyntax
typeName
,
EqualsValueClauseSyntax
initializer
,
SemanticModel
semanticModel
,
OptionSet
optionSet
,
CancellationToken
cancellationToken
)
protected
override
bool
AssignmentSupportsStylePreference
(
SyntaxToken
identifier
,
TypeSyntax
typeName
,
ExpressionSyntax
initializer
,
SemanticModel
semanticModel
,
OptionSet
optionSet
,
CancellationToken
cancellationToken
)
{
// is or contains an anonymous type
// cases :
...
...
@@ -99,7 +119,7 @@ protected override bool AssignmentSupportsStylePreference(SyntaxToken identifier
}
// cannot find type if initializer resolves to an ErrorTypeSymbol
var
initializerTypeInfo
=
semanticModel
.
GetTypeInfo
(
initializer
.
Value
,
cancellationToken
);
var
initializerTypeInfo
=
semanticModel
.
GetTypeInfo
(
initializer
,
cancellationToken
);
return
!
initializerTypeInfo
.
Type
.
IsErrorType
();
}
}
...
...
src/Features/CSharp/Portable/Diagnostics/Analyzers/CSharpUseImplicitTypeDiagnosticAnalyzer.cs
浏览文件 @
1fd082e1
...
...
@@ -87,7 +87,9 @@ protected override bool TryAnalyzeVariableDeclaration(TypeSyntax typeName, Seman
}
var
variable
=
variableDeclaration
.
Variables
.
Single
();
if
(
AssignmentSupportsStylePreference
(
variable
.
Identifier
,
typeName
,
variable
.
Initializer
,
semanticModel
,
optionSet
,
cancellationToken
))
if
(
AssignmentSupportsStylePreference
(
variable
.
Identifier
,
typeName
,
variable
.
Initializer
.
Value
,
semanticModel
,
optionSet
,
cancellationToken
))
{
issueSpan
=
candidateIssueSpan
;
return
true
;
...
...
@@ -110,7 +112,13 @@ protected override bool TryAnalyzeVariableDeclaration(TypeSyntax typeName, Seman
/// false, if implicit typing cannot be used.
/// true, otherwise.
/// </returns>
protected
override
bool
AssignmentSupportsStylePreference
(
SyntaxToken
identifier
,
TypeSyntax
typeName
,
EqualsValueClauseSyntax
initializer
,
SemanticModel
semanticModel
,
OptionSet
optionSet
,
CancellationToken
cancellationToken
)
protected
override
bool
AssignmentSupportsStylePreference
(
SyntaxToken
identifier
,
TypeSyntax
typeName
,
ExpressionSyntax
initializer
,
SemanticModel
semanticModel
,
OptionSet
optionSet
,
CancellationToken
cancellationToken
)
{
var
expression
=
GetInitializerExpression
(
initializer
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录