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

Removing return not sugggested by needless_return when calling some chained string methods #12907

Closed
cyrgani opened this issue Jun 8, 2024 · 2 comments · Fixed by #13214
Closed
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't

Comments

@cyrgani
Copy link

cyrgani commented Jun 8, 2024

Summary

When having a return statement with a call to str::trim, the needless_return list does not warn. Replacing trim withlen in the following example correctly warns.

It seems to happen with other borrowing methods as well, such as in the following example:

fn f() -> String {
	return "".split("").next().unwrap().to_string();
}

Lint Name

needless_return

Reproducer

I tried this code:

fn main(){
	f();
}
fn f() -> String {
	return "".trim().to_string();
}

I expected to see this happen:

warning: unneeded `return` statement
 --> src/main.rs:5:2
  |
5 |     return "".trim().to_string();
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_return
  = note: `#[warn(clippy::needless_return)]` on by default
help: remove `return`
  |
5 -     return "".trim().to_string();
5 +     "".trim().to_string()
  |

Instead, this happened:

No output

Version

rustc 1.78.0 (9b00956e5 2024-04-29)
binary: rustc
commit-hash: 9b00956e56009bab2aa15d7bff10916599e3d6d6
commit-date: 2024-04-29
host: x86_64-unknown-linux-gnu
release: 1.78.0
LLVM version: 18.1.2
@cyrgani cyrgani added C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't labels Jun 8, 2024
@apoisternex
Copy link
Contributor

@rustbot claim

@y21
Copy link
Member

y21 commented Jul 26, 2024

We do need to be careful here because allowing these cases can sometimes also end up as false positives when temporaries are involved, e.g.:

fn f() -> i32 {
  let r = RefCell::new(42);
  return *r.borrow();
}

Trying to replace that return <expr>; with simply <expr> results in a compile error because the temporary that references the local RefCell gets dropped after the local itself

(Though it should be possible to relax the heuristic in the lint that prevents that while stile allowing the repro in the OP)

bors added a commit that referenced this issue Sep 7, 2024
Fix [`needless_return`] false negative

Fixes #12907

changelog: Fix [`needless_return`] false negative when returned expression borrows a value.
@bors bors closed this as completed in af35dcd Sep 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants