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

Use is_empty instead of comparing to empty slice/str #6217

Closed
Urcra opened this issue Oct 24, 2020 · 1 comment · Fixed by #6226
Closed

Use is_empty instead of comparing to empty slice/str #6217

Urcra opened this issue Oct 24, 2020 · 1 comment · Fixed by #6226
Labels
A-lint Area: New lints

Comments

@Urcra
Copy link
Contributor

Urcra commented Oct 24, 2020

What it does

Suggest using is_empty() instead of checking for equality with an empty slice == [] or == "" to be more consistent with similar lints such as len_zero

Categories (optional)

  • Kind: clippy::perf or clippy::style

What is the advantage of the recommended code over the original code

  • Consistency with similar lints such as len_zero so that we suggest lints for both str.len() == 0 and str == ""
  • Performance wise the equality check will also usually take longer to compute compared to is_empty()

Drawbacks

None.

Example

if s == "" {
  ...
}

if arr == [] {
  ...
}

Could be written as:

if s.is_empty() {
  ...
}

if arr.is_empty() {
  ...
}
@Urcra Urcra added the A-lint Area: New lints label Oct 24, 2020
@Urcra
Copy link
Contributor Author

Urcra commented Oct 24, 2020

Bit unsure about with category it should be in. I would probably lean mostly towards style. Since it's a bit hard to evaluate the performance after optimization. Though the performance hit is quite evident in debug mode.

Also not sure if this lint should deal with the case of arr != [] and suggest !arr.is_empty()?

I would like to try and hack on this lint if it seems like something that's useful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant