You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add analyzer: Prevent wrong usage of string Split #47927
api-approvedAPI was approved in API review, it can be implementedarea-System.Runtimecode-analyzerMarks an issue that suggests a Roslyn analyzercode-fixerMarks an issue that suggests a Roslyn code fixerin-prThere is an active PR which will close this issue when it is merged
Consider the following usage of Split on a string where you want to split on space and tab: var split = s.Split(' ', '\t'); (which works fine)
If you now get to the conclusion to remove empty entries, it is tempting to do this: var split = s.Split(' ', '\t', StringSplitOptions.RemoveEmptyEntries);
But this won't give the expected result and will result in a bug in the application, because the second char is converted to an int and used as the count argument for this overload: public string[] Split(char separator, int count, StringSplitOptions options = StringSplitOptions.None);
Describe suggestions on how to achieve the rule
To prevent this, an analyzer should detect that a char is passed to the count parameter of this specific overload and should warn that it won't split on that char.
A codefix can provide a conversion to the following: var split = s.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
(btw. it would be good to also have a template for analyzers in this repository, not only in the roslyn-analyzers repo)
Reacted by Stefan, strangeman375, Buyaa Namnan, Unknown6656, Tan90909090, NN and Cole Tobin
Activity
ghost added
untriagedNew issue has not been triaged by the area owner
A more generalized form of this analyzer would be "You used a char, but it got widened to an int when you passed it to this method."
Not sure how many false positives such an analyzer would produce. It's common to perform integer arithmetic on chars; e.g., (char)('a' + offset) to generate an output in the range a..z when offset is known to be 0..25. But I wouldn't imagine it's all that common to pass literal chars (or standalone char locals) to APIs that expect a wider data type.
Since it's possible to pass a char where a method takes an int, and it gets casted silently, it makes sense that we warn the user to make sure they intended to pass a char array instead.
Analyzer: small
Fixer: small
Suggested category: Reliability
Suggested severity: Warning - The user should manually suppress it in the rare case where they actually intended to get a char casted as int, as described by @GrabYourPitchforks above.
Suggested milestone: Future
Example:
stringtxt="bla bla";string[]splitted1=txt.Split(' ','\t');// OK because argument is "params char[] separators"// BEFORE// Second argument is passed as int because "params" can only be used for the last argumentstring[]splitted2=txt.Split(' ','\t',StringSplitOptions.RemoveEmptyEntries);// AFTER// Suggested fix: Wrap chars in array of charsstring[]splitted2=txt.Split(newchar[]{' ','\t'},StringSplitOptions.RemoveEmptyEntries);// DO NOT FLAG// Make sure to detect the data type of the second argumentcharc=' ';intcount=5;string[]splitted3=txt.Split(c,count,StringSplitOptions.RemoveEmptyEntries);
Reacted by Mrxx99 and AraHaan
ghost added
needs-further-triageIssue has been initially triaged, but needs deeper consideration or reconsideration
Using a messaging along the lines of "suspicious cast", having the rule identify calls to string.Split or StringBuilder..ctor that involves an implicit conversion from char to int seems like goodness (and we may add more specific API over time). All of the methods/constructors that we group together in this rule will use the same diagnostic ID.
There are some generalizations (any implicit char->int in an argument; or that but only when there's an overload that takes char/string) which would be good to follow up on, but they're out of scope for this issue.
Category: Correctness
Severity: Warning
Reacted by Mrxx99 and Joe4evr
Reacted by Mrxx99
ghost added
needs-further-triageIssue has been initially triaged, but needs deeper consideration or reconsideration
api-approvedAPI was approved in API review, it can be implementedarea-System.Runtimecode-analyzerMarks an issue that suggests a Roslyn analyzercode-fixerMarks an issue that suggests a Roslyn code fixerin-prThere is an active PR which will close this issue when it is merged
Activity
GrabYourPitchforks commentedon Feb 5, 2021
A more generalized form of this analyzer would be "You used a char, but it got widened to an int when you passed it to this method."
Not sure how many false positives such an analyzer would produce. It's common to perform integer arithmetic on chars; e.g.,
(char)('a' + offset)to generate an output in the rangea..zwhen offset is known to be0..25. But I wouldn't imagine it's all that common to pass literal chars (or standalone char locals) to APIs that expect a wider data type.See also #46103.
joperezr commentedon Feb 9, 2021
cc: @carlossanlop FYI
carlossanlop commentedon Feb 23, 2021
Since it's possible to pass a
charwhere a method takes anint, and it gets casted silently, it makes sense that we warn the user to make sure they intended to pass a char array instead.Analyzer: small
Fixer: small
Suggested category: Reliability
Suggested severity: Warning - The user should manually suppress it in the rare case where they actually intended to get a
charcasted asint, as described by @GrabYourPitchforks above.Suggested milestone: Future
Example:
bartonjs commentedon Jul 27, 2021
Video
Using a messaging along the lines of "suspicious cast", having the rule identify calls to string.Split or StringBuilder..ctor that involves an implicit conversion from char to int seems like goodness (and we may add more specific API over time). All of the methods/constructors that we group together in this rule will use the same diagnostic ID.
There are some generalizations (any implicit char->int in an argument; or that but only when there's an overload that takes char/string) which would be good to follow up on, but they're out of scope for this issue.
NewellClark commentedon Sep 9, 2021
I would like to be assigned to this, please.