-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Analyzer/fixer proposal: Collapse multiple Path.Combine
or Path.Join
in a row
#62057
Comments
Tagging subscribers to this area: @dotnet/area-system-io Issue DetailsWhen generating multiple combined/joined path segments in a row, where each depends on the previous one (excluding the first one of course), and only the last resulting segment is the one being properly consumed somewhere else, then they can be collapsed into a single invocation of The maximum number of segments in a row that can be fixed are 3, since the largest Flag// Before
string first = Path.Join("folder", "another"); // first is used only as argument of second
string second = Path.Join(first, "onemore");
// After
string second = Path.Join("folder", "another", "onemore");
// Before
string first = Path.Join("folder", "another"); // first is used only as argument of second
string second = Path.Join(first, "onemore"); // second is used only as argument of third
string third = Path.Join(second, "final");
// After
string third = Path.Join("folder", "another", "onemore", "final"); // 4 strings is the largest overload available // Before
string first = Path.Join("folder", "another");
string second = Path.Join(first, "onemore");
string third = Path.Join(second, "yetanother");
string fourth = Path.Join(third, "final");
// After: Can only collapse 3 strings, pick the last 3
string first = Path.Join("folder", "another");
string fourth = Path.Join(first, "onemore", "yetanother", "final"); Do not flagstring first = Path.Join("folder", "another");
string second = Path.Join(first, "final");
Console.WriteLine("{0}, {1}", first, second); // The last segment should be the only one being consumed cc @buyaa-n
|
Thanks, @carlossanlop I have a few questions/comments:
|
|
Suggested severity: Info
Suggested category: Reliability
When generating multiple combined/joined path segments in a row, where each depends on the previous one (excluding the first one of course), and only the last resulting segment is the one being properly consumed somewhere else, then they can be collapsed into a single invocation of
Combine
orJoin
.The maximum number of segments in a row that can be fixed are 3, since the largestCombine
/Join
overloads take 4 parameters.These are the APIs that can benefit from the analyzer:
An additional improvement: whenever possible, switch to using the
ReadOnlySpan<char>
overload instead of thestring
overload, if the number of arguments allows it.Flag
params
overloadJoin
andTryJoin
overloads that takeReadOnlySpan<char>
parameters,Join
can only collapse 4,TryJoin
only 3Do not flag
Combine
, the analyzer should not flag cases where there are potentialnull
arguments.cc @buyaa-n
The text was updated successfully, but these errors were encountered: