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

Support rewriting string.Remove in IDE0057 "Use range operator" #76091

Closed
reflectronic opened this issue Nov 26, 2024 · 2 comments · Fixed by #76129
Closed

Support rewriting string.Remove in IDE0057 "Use range operator" #76091

reflectronic opened this issue Nov 26, 2024 · 2 comments · Fixed by #76129

Comments

@reflectronic
Copy link
Contributor

reflectronic commented Nov 26, 2024

The IDE0057 code style analyzer suggests replacing calls to string.Substring with the range operator. In some cases, calls to string.Remove can also be replaced with the slice syntax. The analyzer should suggest replacing such calls as well.

The following forms are equivalent:

  • string.Remove(string.Length - x)
  • string.Remove(string.Length - x, x)
  • string.Substring(0, string.Length - x)
  • string[..^x]

And:

  • string.Remove(0, string.Length - x)
  • string.Substring(string.Length - x)
  • string[^x..]

And:

  • string.Remove(x, string.Length - x)
  • string.Substring(0, x)
  • string.Remove(x)
  • string[..x]

And:

  • string.Remove(0, x)
  • string.Substring(x)
  • string[x..]

And:

  • string.Remove(x, 0)
  • string

I believe there are no more cases where string.Remove may be rewritten as a slice.

@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged Issues and PRs which have not yet been triaged by a lead label Nov 26, 2024
@CyrusNajmabadi CyrusNajmabadi self-assigned this Nov 26, 2024
@arunchndr arunchndr removed the untriaged Issues and PRs which have not yet been triaged by a lead label Nov 26, 2024
@arunchndr arunchndr added this to the Backlog milestone Nov 26, 2024
@CyrusNajmabadi
Copy link
Member

Image

This doesn't seem to be the case. For example:

        var v = "the quick brown fox jumped over the lazy dog";
        Console.WriteLine(v.Remove(10));
        Console.WriteLine(v[..^10]);

@reflectronic
Copy link
Contributor Author

Sorry, I meant to write string.Remove(string.Length - x) there :-(

string.Remove(x) should be next to string[..x]

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

Successfully merging a pull request may close this issue.

3 participants