Skip to content

Fixer for https://github.com/dotnet/runtime/issues/33785#3610

Merged
pgovind merged 7 commits intodotnet:masterfrom
pgovind:StringContainsFixer
May 20, 2020
Merged

Fixer for https://github.com/dotnet/runtime/issues/33785#3610
pgovind merged 7 commits intodotnet:masterfrom
pgovind:StringContainsFixer

Conversation

@pgovind
Copy link

@pgovind pgovind commented May 7, 2020

Offers a code-fix for the following cases when they appear inside a binary expression such as if (string.IndexOf() == -1):

String.IndexOf(char) -> string.Contains(char)
String.IndexOf(string) -> string.Contains(string, StringComparison.CurrentCulture)
String.IndexOf(string/char, StringComparison.Ordinal) -> string.Contains(string/char)
String.IndexOf(string/char, NON StringComparison.Ordinal) -> string.Contains(string/char, NON StringComparison.Ordinal)

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:

int index = str.IndexOf("AString");
if (index == -1)

@pgovind pgovind requested a review from a team as a code owner May 7, 2020 18:03
pgovind pushed a commit to pgovind/runtime that referenced this pull request May 7, 2020
@codecov
Copy link

codecov bot commented May 11, 2020

Codecov Report

Merging #3610 into master will decrease coverage by 0.02%.
The diff coverage is 89.94%.

@@            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     

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add tests with named arguments with different combination/ordering of arguments in input:

  1. if ([|str.IndexOf(value: ""This"", comparison)" + operatorKind + value + @"|])
  2. if ([|str.IndexOf(""This"", comparisonType: comparison)" + operatorKind + value + @"|])
  3. if ([|str.IndexOf(value: ""This"", comparisonType: comparison)" + operatorKind + value + @"|])
  4. if ([|str.IndexOf(comparisonType: comparison, value: ""This"")" + operatorKind + value + @"|])

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. The only issue was for case 3 in VB. I've commented in the unit test

@mavasani
Copy link

int index = str.IndexOf("AString");
if (index == -1)

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.

@pgovind pgovind force-pushed the StringContainsFixer branch from dd6d97d to 57d5440 Compare May 20, 2020 02:42
@codecov
Copy link

codecov bot commented May 20, 2020

Codecov Report

Merging #3610 into master will increase coverage by 0.48%.
The diff coverage is 89.94%.

@@            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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link

@mavasani mavasani May 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good - feel free to file a tracking issue for VB. We can take a look at it based on customer feedback.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@codecov
Copy link

codecov bot commented May 20, 2020

Codecov Report

Merging #3610 into master will increase coverage by 0.48%.
The diff coverage is 89.94%.

@@            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
Copy link

codecov bot commented May 20, 2020

Codecov Report

Merging #3610 into master will increase coverage by 0.00%.
The diff coverage is 98.21%.

@@           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     

@pgovind
Copy link
Author

pgovind commented May 20, 2020

Thanks for the review @mavasani

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants