-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add new lint unnecessary_wrap
#6070
Conversation
r? @flip1995 (rust_highfive has picked a reviewer for you, use r? to override) |
This lint looks really similar to the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with @mikerite, that the visitor implemented here is pretty similar to bind_instead_of_map
. I also think, that the bind_instead_of_map
visitor is way more complicated than it has to be though.
Anyway, you can at least reuse the visitor of bind_instead_of_map
and then use this code to collect all the return paths:
rust-clippy/clippy_lints/src/methods/bind_instead_of_map.rs
Lines 129 to 142 in 78fbb04
if_chain! { | |
if !in_macro(ret_expr.span); | |
if let hir::ExprKind::Call(ref func_path, ref args) = ret_expr.kind; | |
if let hir::ExprKind::Path(ref qpath) = func_path.kind; | |
if match_qpath(qpath, Self::BAD_VARIANT_QPATH); | |
if args.len() == 1; | |
if !contains_return(&args[0]); | |
then { | |
suggs.push((ret_expr.span, args[0].span.source_callsite())); | |
true | |
} else { | |
false | |
} | |
} |
rustfix doesn't work with multipart suggestions, so don't worry about that. (You can use diag.multipart_suggestion()
directly instead of the one from utils, since the utils one is just a useless wrapper, we should remove sometime)
Sorry for the late reply, I could not use computers as I was replacing my desk.
Got it, made the changes accordingly. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer if you could split the suggestion in two parts, so that it looks like the following:
error: this function returns unnecessarily wrapping data
--> $DIR/unnecessary_wrap.rs:39:1
|
LL | / fn func4() -> Option<i32> {
LL | | Some(1)
LL | | }
| |_^
|
help: remove `Option` from the return type...
|
LL | fn func4() -> i32 {
| ^^^
help: ...and change the returning expressions
|
LL | 1
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI it looks like that rust-lang/rust-clippy repo follows rustc no-merge policy. For example #5694 (comment).
Oops, thanks for pointing out. |
b1205bf
to
c4e24c8
Compare
122c4a6
to
97868e3
Compare
Some lines in clippy itself are linted and I noticed that this lint should not check fns named |
fa1763f
to
0b24295
Compare
☔ The latest upstream changes (presumably #6267) made this pull request unmergeable. Please resolve the merge conflicts. Note that reviewers usually do not review pull requests until merge conflicts are resolved! Once you resolve the conflicts, you should change the labels applied by bors to indicate that your PR is ready for review. Post this as a comment to change the labels:
|
0b24295
to
d9ac655
Compare
@rustbot modify labels: +S-waiting-on-review -S-waiting-on-author |
@bors r+ Thanks! |
📌 Commit 2881bbd has been approved by |
26e7bec
to
c7445d7
Compare
@bors r+ Thanks! |
📌 Commit c7445d7 has been approved by |
Add new lint `unnecessary_wrap` Fixes #5969 changelog: New lint [`unnecessary_wraps`]
💔 Test failed - checks-action_test |
@bors retry |
Add new lint `unnecessary_wrap` Fixes #5969 changelog: New lint [`unnecessary_wraps`]
💔 Test failed - checks-action_test |
@bors retry |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
Maybe it's just me, but this lint seems awfully aggressive. There are plenty of reasons an interface might be written to return an For instance, I'm very frequently factoring out the last step of initialization into a helper method; the Please move this to pedantic. |
I'm not sure I follow you. Wrapping an infallible call in It seems to me that the situation you describe is best solved with this: impl Foo {
pub fn new(args: Args) -> Result<Self, Error> {
if args.are_bad() {
Err(...)
} else {
Ok(Foo::private_new(args))
}
}
fn private_new(args: Args) -> Self { ... }
} But I feel like I am probably missing something! |
@emilk Personally, I don't think it's the role of linting to make these sorts of invasive signature requirements by default. The authors seem to agree, at least in part, given that the lint doesn't apply to functions with a visiblity of That being said, I don't think I have the bandwidth to pursue this further. Thanks for your consideration. |
Remove `Result` from return types in tests of `opentelemetry-datadog`, `opentelemetry-jaeger`, `opentelemetry-otlp`, `opentelemetry-zipkin`. Reference: rust-lang/rust-clippy#6070
Fixes #5969
changelog: New lint [
unnecessary_wraps
]