Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
aa4ecc07
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,发现更多精彩内容 >>
未验证
提交
aa4ecc07
编写于
8月 10, 2020
作者:
J
Jonathon Marolf
提交者:
GitHub
8月 10, 2020
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #44951 from m-redding/bug-43202
上级
8331c782
4fe6582f
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
165 addition
and
3 deletion
+165
-3
src/Analyzers/CSharp/Analyzers/UseIndexOrRangeOperator/CSharpUseRangeOperatorDiagnosticAnalyzer.cs
...RangeOperator/CSharpUseRangeOperatorDiagnosticAnalyzer.cs
+17
-1
src/Analyzers/CSharp/Tests/UseIndexOrRangeOperator/UseRangeOperatorTests.cs
...rp/Tests/UseIndexOrRangeOperator/UseRangeOperatorTests.cs
+148
-2
未找到文件。
src/Analyzers/CSharp/Analyzers/UseIndexOrRangeOperator/CSharpUseRangeOperatorDiagnosticAnalyzer.cs
浏览文件 @
aa4ecc07
...
...
@@ -7,10 +7,12 @@
using
System.Threading
;
using
Microsoft.CodeAnalysis.CodeStyle
;
using
Microsoft.CodeAnalysis.CSharp.CodeStyle
;
using
Microsoft.CodeAnalysis.CSharp.Extensions
;
using
Microsoft.CodeAnalysis.CSharp.LanguageServices
;
using
Microsoft.CodeAnalysis.CSharp.Syntax
;
using
Microsoft.CodeAnalysis.Diagnostics
;
using
Microsoft.CodeAnalysis.Operations
;
using
Microsoft.CodeAnalysis.Shared.Extensions
;
using
Microsoft.CodeAnalysis.Text
;
namespace
Microsoft.CodeAnalysis.CSharp.UseIndexOrRangeOperator
...
...
@@ -77,7 +79,6 @@ protected override void InitializeWorker(AnalysisContext context)
{
return
;
}
context
.
ReportDiagnostic
(
CreateDiagnostic
(
resultOpt
.
Value
));
}
...
...
@@ -171,6 +172,14 @@ protected override void InitializeWorker(AnalysisContext context)
return
null
;
}
var
indexer
=
GetIndexer
(
targetMethod
.
ContainingType
,
infoCache
.
RangeType
,
targetMethod
.
ContainingType
);
// Need to make sure that if the target method is being written to, that the indexer returns a ref, is a read/write property,
// or the syntax allows for the slice method to be run
if
(
invocation
.
Syntax
.
IsLeftSideOfAnyAssignExpression
()
&&
indexer
!=
null
&&
IsWriteableIndexer
(
invocation
,
indexer
))
{
return
null
;
}
// See if we have: (start, end - start). Specifically where the start operation it the
// same as the right side of the subtraction.
var
startOperation
=
invocation
.
Arguments
[
0
].
Value
;
...
...
@@ -224,5 +233,12 @@ private Diagnostic CreateDiagnostic(Result result)
private
static
bool
IsConstantInt32
(
IOperation
operation
)
=>
operation
.
ConstantValue
.
HasValue
&&
operation
.
ConstantValue
.
Value
is
int
;
private
static
bool
IsWriteableIndexer
(
IInvocationOperation
invocation
,
IPropertySymbol
indexer
)
{
var
refReturnMismatch
=
indexer
.
ReturnsByRef
!=
invocation
.
TargetMethod
.
ReturnsByRef
;
var
indexerIsReadWrite
=
indexer
.
IsWriteableFieldOrProperty
();
return
refReturnMismatch
&&
!
indexerIsReadWrite
;
}
}
}
src/Analyzers/CSharp/Tests/UseIndexOrRangeOperator/UseRangeOperatorTests.cs
浏览文件 @
aa4ecc07
...
...
@@ -435,7 +435,6 @@ void Goo(S s)
s[1..^1] = default;
}
}"
;
await
new
VerifyCS
.
Test
{
ReferenceAssemblies
=
ReferenceAssemblies
.
NetCore
.
NetCoreApp31
,
...
...
@@ -604,6 +603,154 @@ void Goo(Span<int> s)
}.
RunAsync
();
}
[
WorkItem
(
43202
,
"https://github.com/dotnet/roslyn/issues/43202"
)]
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsUseRangeOperator
)]
public
async
Task
TestWritableType
()
{
var
source
=
@"
using System;
struct S {
public ref S Slice(int start, int length) => throw null;
public int Length { get; }
public S this[System.Range r] { get => default; }
}
class C
{
void Goo(S s)
{
s.Slice(1, s.Length - 2) = default;
}
}"
;
await
new
VerifyCS
.
Test
{
ReferenceAssemblies
=
ReferenceAssemblies
.
NetCore
.
NetCoreApp31
,
TestCode
=
source
,
FixedCode
=
source
,
}.
RunAsync
();
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsUseRangeOperator
)]
public
async
Task
TestReturnByRef
()
{
var
source
=
@"
struct S { public ref S Slice(int start, int length) => throw null; public int Length { get; } public S this[System.Range r] { get => throw null; } }
class C
{
void Goo(S s)
{
var x = s.Slice([|1, s.Length - 2|]);
}
}"
;
var
fixedSource
=
@"
struct S { public ref S Slice(int start, int length) => throw null; public int Length { get; } public S this[System.Range r] { get => throw null; } }
class C
{
void Goo(S s)
{
var x = s[1..^1];
}
}"
;
await
new
VerifyCS
.
Test
{
ReferenceAssemblies
=
ReferenceAssemblies
.
NetCore
.
NetCoreApp31
,
TestCode
=
source
,
FixedCode
=
fixedSource
,
}.
RunAsync
();
}
[
WorkItem
(
43202
,
"https://github.com/dotnet/roslyn/issues/43202"
)]
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsUseRangeOperator
)]
public
async
Task
TestIntWritableType
()
{
var
source
=
@"
using System;
struct S {
public ref S Slice(int start, int length) => throw null;
public int Length { get; }
public S this[int r] { get => default; }
}
class C
{
void Goo(S s)
{
s.Slice([|1, s.Length - 2|]) = default;
}
}"
;
var
fixedSource
=
@"
using System;
struct S {
public ref S Slice(int start, int length) => throw null;
public int Length { get; }
public S this[int r] { get => default; }
}
class C
{
void Goo(S s)
{
s[1..^1] = default;
}
}"
;
await
new
VerifyCS
.
Test
{
ReferenceAssemblies
=
ReferenceAssemblies
.
NetCore
.
NetCoreApp31
,
TestCode
=
source
,
FixedCode
=
fixedSource
,
}.
RunAsync
();
}
[
WorkItem
(
43202
,
"https://github.com/dotnet/roslyn/issues/43202"
)]
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsUseRangeOperator
)]
public
async
Task
TestReadWriteProperty
()
{
var
source
=
@"
using System;
struct S {
public ref S Slice(int start, int length) => throw null;
public int Length { get; }
public S this[System.Range r] { get => default; set { } }
}
class C
{
void Goo(S s)
{
s.Slice([|1, s.Length - 2|]) = default;
}
}"
;
var
fixedSource
=
@"
using System;
struct S {
public ref S Slice(int start, int length) => throw null;
public int Length { get; }
public S this[System.Range r] { get => default; set { } }
}
class C
{
void Goo(S s)
{
s[1..^1] = default;
}
}"
;
await
new
VerifyCS
.
Test
{
ReferenceAssemblies
=
ReferenceAssemblies
.
NetCore
.
NetCoreApp31
,
TestCode
=
source
,
FixedCode
=
fixedSource
,
}.
RunAsync
();
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsUseRangeOperator
)]
public
async
Task
TestWithTypeWithActualSliceMethod3
()
{
...
...
@@ -627,7 +774,6 @@ void Goo(Span<int> s)
var v = s[1..];
}
}"
;
await
new
VerifyCS
.
Test
{
ReferenceAssemblies
=
ReferenceAssemblies
.
NetCore
.
NetCoreApp31
,
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录