-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add new lint map_or_same_constant
#17577
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
Draft
ivanlomeli
wants to merge
1
commit into
rust-lang:master
Choose a base branch
from
ivanlomeli:map_or_same_constant
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| use clippy_utils::consts::ConstEvalCtxt; | ||
| use clippy_utils::diagnostics::span_lint_and_note; | ||
| use clippy_utils::is_from_proc_macro; | ||
| use clippy_utils::res::MaybeDef as _; | ||
| use rustc_hir::{Expr, ExprKind}; | ||
| use rustc_lint::LateContext; | ||
| use rustc_span::sym; | ||
|
|
||
| use super::MAP_OR_SAME_CONSTANT; | ||
|
|
||
| pub(super) fn check<'tcx>( | ||
| cx: &LateContext<'tcx>, | ||
| expr: &'tcx Expr<'tcx>, | ||
| recv: &'tcx Expr<'tcx>, | ||
| def: &'tcx Expr<'tcx>, | ||
| map: &'tcx Expr<'tcx>, | ||
| is_map_or_else: bool, | ||
| ) { | ||
| if expr.span.from_expansion() || is_from_proc_macro(cx, expr) { | ||
| return; | ||
| } | ||
|
|
||
| let recv_ty = cx.typeck_results().expr_ty_adjusted(recv); | ||
| let Some(type_sym) = recv_ty.opt_diag_name(cx) else { | ||
| return; | ||
| }; | ||
| if !matches!(type_sym, sym::Option | sym::Result) { | ||
| return; | ||
| } | ||
|
|
||
| let const_eval = ConstEvalCtxt::new(cx); | ||
|
|
||
| let def_const = if is_map_or_else { | ||
| if let ExprKind::Closure(closure) = def.kind { | ||
| let body = cx.tcx.hir_body(closure.body); | ||
| const_eval.eval(body.value.peel_blocks()) | ||
| } else { | ||
| None | ||
| } | ||
| } else { | ||
| const_eval.eval(def) | ||
| }; | ||
|
|
||
| let Some(def_const) = def_const else { | ||
| return; | ||
| }; | ||
|
|
||
| let map_const = if let ExprKind::Closure(closure) = map.kind { | ||
| let body = cx.tcx.hir_body(closure.body); | ||
| const_eval.eval(body.value.peel_blocks()) | ||
| } else { | ||
| None | ||
| }; | ||
|
|
||
| if let Some(map_const) = map_const | ||
| && def_const == map_const | ||
| { | ||
| let method_name = if is_map_or_else { "map_or_else" } else { "map_or" }; | ||
| span_lint_and_note( | ||
| cx, | ||
| MAP_OR_SAME_CONSTANT, | ||
| expr.span, | ||
| format!("both branches of `{method_name}` return the same constant value"), | ||
| None, | ||
| format!("this `{method_name}` always evaluates to the same value, regardless of the `{type_sym}` variant"), | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| #![warn(clippy::map_or_same_constant)] | ||
| #![allow(clippy::unnecessary_map_or)] | ||
|
|
||
| fn main() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking of the code, all of them are covered.
|
||
| let opt: Option<i32> = Some(42); | ||
| let res: Result<i32, ()> = Ok(42); | ||
|
|
||
| // Should lint: Option::map_or with identical constants | ||
| let _ = opt.map_or(false, |_| false); | ||
| //~^ map_or_same_constant | ||
| let _ = opt.map_or(true, |_| true); | ||
| //~^ map_or_same_constant | ||
| let _ = opt.map_or(0, |_| 0); | ||
| //~^ map_or_same_constant | ||
| let _ = opt.map_or("foo", |_| "foo"); | ||
| //~^ map_or_same_constant | ||
|
|
||
| // Should lint: Result::map_or with identical constants | ||
| let _ = res.map_or(false, |_| false); | ||
| //~^ map_or_same_constant | ||
| let _ = res.map_or(true, |_| true); | ||
| //~^ map_or_same_constant | ||
| let _ = res.map_or(123, |_| 123); | ||
| //~^ map_or_same_constant | ||
|
|
||
| // Should lint: Option::map_or_else with identical constants | ||
| let _ = opt.map_or_else(|| true, |_| true); | ||
| //~^ map_or_same_constant | ||
| let _ = opt.map_or_else(|| 0, |_| 0); | ||
| //~^ map_or_same_constant | ||
|
|
||
| // Should lint: Result::map_or_else with identical constants | ||
| let _ = res.map_or_else(|_| false, |_| false); | ||
| //~^ map_or_same_constant | ||
| let _ = res.map_or_else(|_| "same", |_| "same"); | ||
| //~^ map_or_same_constant | ||
|
|
||
| // Should NOT lint: different constants | ||
| let _ = opt.map_or(false, |_| true); | ||
| let _ = res.map_or(0, |_| 1); | ||
| let _ = opt.map_or_else(|| false, |_| true); | ||
|
|
||
| // Should NOT lint: dynamic values | ||
| let _ = opt.map_or(0, |x| x + 1); | ||
| let _ = res.map_or_else(|_| 0, |x| x * 2); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| error: both branches of `map_or` return the same constant value | ||
| --> tests/ui/map_or_same_constant.rs:9:13 | ||
| | | ||
| LL | let _ = opt.map_or(false, |_| false); | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| = note: this `map_or` always evaluates to the same value, regardless of the `Option` variant | ||
| = note: `-D clippy::map-or-same-constant` implied by `-D warnings` | ||
| = help: to override `-D warnings` add `#[allow(clippy::map_or_same_constant)]` | ||
|
|
||
| error: both branches of `map_or` return the same constant value | ||
| --> tests/ui/map_or_same_constant.rs:11:13 | ||
| | | ||
| LL | let _ = opt.map_or(true, |_| true); | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| = note: this `map_or` always evaluates to the same value, regardless of the `Option` variant | ||
|
|
||
| error: both branches of `map_or` return the same constant value | ||
| --> tests/ui/map_or_same_constant.rs:13:13 | ||
| | | ||
| LL | let _ = opt.map_or(0, |_| 0); | ||
| | ^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| = note: this `map_or` always evaluates to the same value, regardless of the `Option` variant | ||
|
|
||
| error: both branches of `map_or` return the same constant value | ||
| --> tests/ui/map_or_same_constant.rs:15:13 | ||
| | | ||
| LL | let _ = opt.map_or("foo", |_| "foo"); | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| = note: this `map_or` always evaluates to the same value, regardless of the `Option` variant | ||
|
|
||
| error: both branches of `map_or` return the same constant value | ||
| --> tests/ui/map_or_same_constant.rs:19:13 | ||
| | | ||
| LL | let _ = res.map_or(false, |_| false); | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| = note: this `map_or` always evaluates to the same value, regardless of the `Result` variant | ||
|
|
||
| error: both branches of `map_or` return the same constant value | ||
| --> tests/ui/map_or_same_constant.rs:21:13 | ||
| | | ||
| LL | let _ = res.map_or(true, |_| true); | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| = note: this `map_or` always evaluates to the same value, regardless of the `Result` variant | ||
|
|
||
| error: both branches of `map_or` return the same constant value | ||
| --> tests/ui/map_or_same_constant.rs:23:13 | ||
| | | ||
| LL | let _ = res.map_or(123, |_| 123); | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| = note: this `map_or` always evaluates to the same value, regardless of the `Result` variant | ||
|
|
||
| error: both branches of `map_or_else` return the same constant value | ||
| --> tests/ui/map_or_same_constant.rs:27:13 | ||
| | | ||
| LL | let _ = opt.map_or_else(|| true, |_| true); | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| = note: this `map_or_else` always evaluates to the same value, regardless of the `Option` variant | ||
|
|
||
| error: both branches of `map_or_else` return the same constant value | ||
| --> tests/ui/map_or_same_constant.rs:29:13 | ||
| | | ||
| LL | let _ = opt.map_or_else(|| 0, |_| 0); | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| = note: this `map_or_else` always evaluates to the same value, regardless of the `Option` variant | ||
|
|
||
| error: both branches of `map_or_else` return the same constant value | ||
| --> tests/ui/map_or_same_constant.rs:33:13 | ||
| | | ||
| LL | let _ = res.map_or_else(|_| false, |_| false); | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| = note: this `map_or_else` always evaluates to the same value, regardless of the `Result` variant | ||
|
|
||
| error: both branches of `map_or_else` return the same constant value | ||
| --> tests/ui/map_or_same_constant.rs:35:13 | ||
| | | ||
| LL | let _ = res.map_or_else(|_| "same", |_| "same"); | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| = note: this `map_or_else` always evaluates to the same value, regardless of the `Result` variant | ||
|
|
||
| error: aborting due to 11 previous errors | ||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Could you expect instead of allow instead?
Also, when do they overlap, can we improve this?
Ideally, the same code should not lint twice, since that sounds like a source for confusion, or?