Fixer for https://github.com/dotnet/runtime/issues/33785#3610
Fixer for https://github.com/dotnet/runtime/issues/33785#3610pgovind merged 7 commits intodotnet:masterfrom
Conversation
Cases tagged by dotnet/roslyn-analyzers#3610
Codecov Report
@@ Coverage Diff @@
## master #3610 +/- ##
==========================================
- Coverage 95.18% 95.15% -0.03%
==========================================
Files 1073 1075 +2
Lines 243974 244377 +403
Branches 15867 16011 +144
==========================================
+ Hits 232218 232537 +319
- Misses 10003 10019 +16
- Partials 1753 1821 +68 |
...tAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/PreferStringContainsOverIndexOf.Fixer.cs
Outdated
Show resolved
Hide resolved
...tAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/PreferStringContainsOverIndexOf.Fixer.cs
Outdated
Show resolved
Hide resolved
...tAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/PreferStringContainsOverIndexOf.Fixer.cs
Outdated
Show resolved
Hide resolved
...tAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/PreferStringContainsOverIndexOf.Fixer.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Can we add tests with named arguments with different combination/ordering of arguments in input:
if ([|str.IndexOf(value: ""This"", comparison)" + operatorKind + value + @"|])if ([|str.IndexOf(""This"", comparisonType: comparison)" + operatorKind + value + @"|])if ([|str.IndexOf(value: ""This"", comparisonType: comparison)" + operatorKind + value + @"|])if ([|str.IndexOf(comparisonType: comparison, value: ""This"")" + operatorKind + value + @"|])
There was a problem hiding this comment.
Done. The only issue was for case 3 in VB. I've commented in the unit test
...tAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/PreferStringContainsOverIndexOf.Fixer.cs
Outdated
Show resolved
Hide resolved
...tAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/PreferStringContainsOverIndexOf.Fixer.cs
Outdated
Show resolved
Hide resolved
...tAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/PreferStringContainsOverIndexOf.Fixer.cs
Outdated
Show resolved
Hide resolved
...tAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/PreferStringContainsOverIndexOf.Fixer.cs
Outdated
Show resolved
Hide resolved
...tAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/PreferStringContainsOverIndexOf.Fixer.cs
Outdated
Show resolved
Hide resolved
Can we file a follow-up issue to handle the simplest case here where index computation is immediately followed by an index check in the next statement? I think it maybe useful, even if it is implemented only for C#. We can certainly wait for feedback before implementing it. |
dd6d97d to
57d5440
Compare
Codecov Report
@@ Coverage Diff @@
## master #3610 +/- ##
==========================================
+ Coverage 94.67% 95.15% +0.48%
==========================================
Files 1073 1075 +2
Lines 244477 244377 -100
Branches 14771 16011 +1240
==========================================
+ Hits 231449 232537 +1088
+ Misses 10999 10019 -980
+ Partials 2029 1821 -208 |
| Class TestClass | ||
| Public Sub Main() | ||
| Dim Str As String = ""This is a statement"" | ||
| If Not Str.Contains(value:= " + startQuote + input + startQuote + vbCharLiteral + @", comparisonType:= System.StringComparison.OrdinalIgnoreCase) Then |
There was a problem hiding this comment.
Note that the arguments are reversed here. I don't understand why this happens, but in the debugger I can see that invocationOperation.SyntaxNode (in the fixer code) has its arguments in the (comparisonType, value) order, but invocationOperation.Arguments returns it as (value, comparisonType). It seems to work fine for C# though
There was a problem hiding this comment.
@pgovind Argument order in the IOperation world is in the order of runtime evaluation, does not depend on source ordering, see https://github.com/dotnet/roslyn/blob/master/src/Compilers/Core/Portable/Generated/Operations.Generated.cs#L639-L646
There was a problem hiding this comment.
Sounds good - feel free to file a tracking issue for VB. We can take a look at it based on customer feedback.
Codecov Report
@@ Coverage Diff @@
## master #3610 +/- ##
==========================================
+ Coverage 94.67% 95.15% +0.48%
==========================================
Files 1073 1075 +2
Lines 244504 244377 -127
Branches 14773 16011 +1238
==========================================
+ Hits 231480 232537 +1057
+ Misses 11000 10019 -981
+ Partials 2024 1821 -203 |
Codecov Report
@@ Coverage Diff @@
## master #3610 +/- ##
========================================
Coverage 94.67% 94.67%
========================================
Files 1073 1074 +1
Lines 244504 244939 +435
Branches 14773 14787 +14
========================================
+ Hits 231480 231896 +416
- Misses 11000 11010 +10
- Partials 2024 2033 +9 |
|
Thanks for the review @mavasani |
Offers a code-fix for the following cases when they appear inside a binary expression such as
if (string.IndexOf() == -1):The analyzer also tags the following case, but @mavasani recommended against offering a code-fix here(since it involves more data flow analysis than roslyn provides currently). Note: The analyzer will still fire: