Skip to content
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

A new rule to warn against nesting Path.Combine. #226

Open
brian-reichle opened this issue Jul 22, 2024 · 2 comments
Open

A new rule to warn against nesting Path.Combine. #226

brian-reichle opened this issue Jul 22, 2024 · 2 comments

Comments

@brian-reichle
Copy link
Contributor

I encountered a couple of places where someone nested Path.Combine and thought we should probably have a rule that guides people to inline them.

so:

var path = Path.Combine(Path.Combine(rootPath, "Foo"), "Bar");

becomes:

var path = Path.Combine(rootPath, "Foo", "Bar");
@yaakov-h
Copy link
Member

Looking over the docs for this I noticed that we now also have Path.Join in modern .NET. Is there any scenarios where we should be suggesting that instead, perhaps as a separate rule?

@brian-reichle
Copy link
Contributor Author

If the values you are joining are all strings, then I don't think it matters. But if you are getting one or more of the elements from a substring (or one of the Path.GetXXX() methods that have span overloads), then Path.Join would be preferable.
eg.

var path = Path.Combine(Path.GetDirectoryName(related), "Foo.xml");

becomes

var path = Path.Join(Path.GetDirectoryName(related.AsSpan()), "Foo.xml".AsSpan());

Might be hard to actually detect this though.

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

No branches or pull requests

2 participants