Skip to content

Commit

Permalink
Auto merge of rust-lang#9551 - kraktus:unnecessary_lazy_evaluations, …
Browse files Browse the repository at this point in the history
…r=xFrednet

[`unnecessary_lazy_evaluations`] Do not suggest switching to early evaluation when type has custom `Drop`

fix rust-lang#9427

changelog: [`unnecessary_lazy_evaluations`] Do not suggest switching to early evaluation when type has custom `Drop`
  • Loading branch information
bors committed Sep 29, 2022
2 parents ed9c0e3 + 314d57a commit 5e8a4b6
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 35 deletions.
12 changes: 11 additions & 1 deletion clippy_utils/src/eager_or_lazy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,17 @@ fn expr_eagerness<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) -> EagernessS
},
args,
) => match self.cx.qpath_res(path, hir_id) {
Res::Def(DefKind::Ctor(..) | DefKind::Variant, _) | Res::SelfCtor(_) => (),
Res::Def(DefKind::Ctor(..) | DefKind::Variant, _) | Res::SelfCtor(_) => {
if self
.cx
.typeck_results()
.expr_ty(e)
.has_significant_drop(self.cx.tcx, self.cx.param_env)
{
self.eagerness = Lazy;
return;
}
},
Res::Def(_, id) if self.cx.tcx.is_promotable_const_fn(id) => (),
// No need to walk the arguments here, `is_const_evaluatable` already did
Res::Def(..) if is_const_evaluatable(self.cx, e) => {
Expand Down
11 changes: 11 additions & 0 deletions tests/ui/unnecessary_lazy_eval.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ fn some_call<T: Default>() -> T {
T::default()
}

struct Issue9427(i32);

impl Drop for Issue9427 {
fn drop(&mut self) {
println!("{}", self.0);
}
}

fn main() {
let astronomers_pi = 10;
let ext_arr: [usize; 1] = [2];
Expand Down Expand Up @@ -73,6 +81,9 @@ fn main() {
let _ = deep.0.or_else(|| some_call());
let _ = opt.ok_or_else(|| ext_arr[0]);

// Should not lint - bool
let _ = (0 == 1).then(|| Issue9427(0)); // Issue9427 has a significant drop

// should not lint, bind_instead_of_map takes priority
let _ = Some(10).and_then(|idx| Some(ext_arr[idx]));
let _ = Some(10).and_then(|idx| Some(idx));
Expand Down
11 changes: 11 additions & 0 deletions tests/ui/unnecessary_lazy_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ fn some_call<T: Default>() -> T {
T::default()
}

struct Issue9427(i32);

impl Drop for Issue9427 {
fn drop(&mut self) {
println!("{}", self.0);
}
}

fn main() {
let astronomers_pi = 10;
let ext_arr: [usize; 1] = [2];
Expand Down Expand Up @@ -73,6 +81,9 @@ fn main() {
let _ = deep.0.or_else(|| some_call());
let _ = opt.ok_or_else(|| ext_arr[0]);

// Should not lint - bool
let _ = (0 == 1).then(|| Issue9427(0)); // Issue9427 has a significant drop

// should not lint, bind_instead_of_map takes priority
let _ = Some(10).and_then(|idx| Some(ext_arr[idx]));
let _ = Some(10).and_then(|idx| Some(idx));
Expand Down
68 changes: 34 additions & 34 deletions tests/ui/unnecessary_lazy_eval.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: unnecessary closure used to substitute value for `Option::None`
--> $DIR/unnecessary_lazy_eval.rs:36:13
--> $DIR/unnecessary_lazy_eval.rs:44:13
|
LL | let _ = opt.unwrap_or_else(|| 2);
| ^^^^--------------------
Expand All @@ -9,263 +9,263 @@ LL | let _ = opt.unwrap_or_else(|| 2);
= note: `-D clippy::unnecessary-lazy-evaluations` implied by `-D warnings`

error: unnecessary closure used to substitute value for `Option::None`
--> $DIR/unnecessary_lazy_eval.rs:37:13
--> $DIR/unnecessary_lazy_eval.rs:45:13
|
LL | let _ = opt.unwrap_or_else(|| astronomers_pi);
| ^^^^---------------------------------
| |
| help: use `unwrap_or(..)` instead: `unwrap_or(astronomers_pi)`

error: unnecessary closure used to substitute value for `Option::None`
--> $DIR/unnecessary_lazy_eval.rs:38:13
--> $DIR/unnecessary_lazy_eval.rs:46:13
|
LL | let _ = opt.unwrap_or_else(|| ext_str.some_field);
| ^^^^-------------------------------------
| |
| help: use `unwrap_or(..)` instead: `unwrap_or(ext_str.some_field)`

error: unnecessary closure used to substitute value for `Option::None`
--> $DIR/unnecessary_lazy_eval.rs:40:13
--> $DIR/unnecessary_lazy_eval.rs:48:13
|
LL | let _ = opt.and_then(|_| ext_opt);
| ^^^^---------------------
| |
| help: use `and(..)` instead: `and(ext_opt)`

error: unnecessary closure used to substitute value for `Option::None`
--> $DIR/unnecessary_lazy_eval.rs:41:13
--> $DIR/unnecessary_lazy_eval.rs:49:13
|
LL | let _ = opt.or_else(|| ext_opt);
| ^^^^-------------------
| |
| help: use `or(..)` instead: `or(ext_opt)`

error: unnecessary closure used to substitute value for `Option::None`
--> $DIR/unnecessary_lazy_eval.rs:42:13
--> $DIR/unnecessary_lazy_eval.rs:50:13
|
LL | let _ = opt.or_else(|| None);
| ^^^^----------------
| |
| help: use `or(..)` instead: `or(None)`

error: unnecessary closure used to substitute value for `Option::None`
--> $DIR/unnecessary_lazy_eval.rs:43:13
--> $DIR/unnecessary_lazy_eval.rs:51:13
|
LL | let _ = opt.get_or_insert_with(|| 2);
| ^^^^------------------------
| |
| help: use `get_or_insert(..)` instead: `get_or_insert(2)`

error: unnecessary closure used to substitute value for `Option::None`
--> $DIR/unnecessary_lazy_eval.rs:44:13
--> $DIR/unnecessary_lazy_eval.rs:52:13
|
LL | let _ = opt.ok_or_else(|| 2);
| ^^^^----------------
| |
| help: use `ok_or(..)` instead: `ok_or(2)`

error: unnecessary closure used to substitute value for `Option::None`
--> $DIR/unnecessary_lazy_eval.rs:45:13
--> $DIR/unnecessary_lazy_eval.rs:53:13
|
LL | let _ = nested_tuple_opt.unwrap_or_else(|| Some((1, 2)));
| ^^^^^^^^^^^^^^^^^-------------------------------
| |
| help: use `unwrap_or(..)` instead: `unwrap_or(Some((1, 2)))`

error: unnecessary closure used with `bool::then`
--> $DIR/unnecessary_lazy_eval.rs:46:13
--> $DIR/unnecessary_lazy_eval.rs:54:13
|
LL | let _ = cond.then(|| astronomers_pi);
| ^^^^^-----------------------
| |
| help: use `then_some(..)` instead: `then_some(astronomers_pi)`

error: unnecessary closure used to substitute value for `Option::None`
--> $DIR/unnecessary_lazy_eval.rs:49:13
--> $DIR/unnecessary_lazy_eval.rs:57:13
|
LL | let _ = Some(10).unwrap_or_else(|| 2);
| ^^^^^^^^^--------------------
| |
| help: use `unwrap_or(..)` instead: `unwrap_or(2)`

error: unnecessary closure used to substitute value for `Option::None`
--> $DIR/unnecessary_lazy_eval.rs:50:13
--> $DIR/unnecessary_lazy_eval.rs:58:13
|
LL | let _ = Some(10).and_then(|_| ext_opt);
| ^^^^^^^^^---------------------
| |
| help: use `and(..)` instead: `and(ext_opt)`

error: unnecessary closure used to substitute value for `Option::None`
--> $DIR/unnecessary_lazy_eval.rs:51:28
--> $DIR/unnecessary_lazy_eval.rs:59:28
|
LL | let _: Option<usize> = None.or_else(|| ext_opt);
| ^^^^^-------------------
| |
| help: use `or(..)` instead: `or(ext_opt)`

error: unnecessary closure used to substitute value for `Option::None`
--> $DIR/unnecessary_lazy_eval.rs:52:13
--> $DIR/unnecessary_lazy_eval.rs:60:13
|
LL | let _ = None.get_or_insert_with(|| 2);
| ^^^^^------------------------
| |
| help: use `get_or_insert(..)` instead: `get_or_insert(2)`

error: unnecessary closure used to substitute value for `Option::None`
--> $DIR/unnecessary_lazy_eval.rs:53:35
--> $DIR/unnecessary_lazy_eval.rs:61:35
|
LL | let _: Result<usize, usize> = None.ok_or_else(|| 2);
| ^^^^^----------------
| |
| help: use `ok_or(..)` instead: `ok_or(2)`

error: unnecessary closure used to substitute value for `Option::None`
--> $DIR/unnecessary_lazy_eval.rs:54:28
--> $DIR/unnecessary_lazy_eval.rs:62:28
|
LL | let _: Option<usize> = None.or_else(|| None);
| ^^^^^----------------
| |
| help: use `or(..)` instead: `or(None)`

error: unnecessary closure used to substitute value for `Option::None`
--> $DIR/unnecessary_lazy_eval.rs:57:13
--> $DIR/unnecessary_lazy_eval.rs:65:13
|
LL | let _ = deep.0.unwrap_or_else(|| 2);
| ^^^^^^^--------------------
| |
| help: use `unwrap_or(..)` instead: `unwrap_or(2)`

error: unnecessary closure used to substitute value for `Option::None`
--> $DIR/unnecessary_lazy_eval.rs:58:13
--> $DIR/unnecessary_lazy_eval.rs:66:13
|
LL | let _ = deep.0.and_then(|_| ext_opt);
| ^^^^^^^---------------------
| |
| help: use `and(..)` instead: `and(ext_opt)`

error: unnecessary closure used to substitute value for `Option::None`
--> $DIR/unnecessary_lazy_eval.rs:59:13
--> $DIR/unnecessary_lazy_eval.rs:67:13
|
LL | let _ = deep.0.or_else(|| None);
| ^^^^^^^----------------
| |
| help: use `or(..)` instead: `or(None)`

error: unnecessary closure used to substitute value for `Option::None`
--> $DIR/unnecessary_lazy_eval.rs:60:13
--> $DIR/unnecessary_lazy_eval.rs:68:13
|
LL | let _ = deep.0.get_or_insert_with(|| 2);
| ^^^^^^^------------------------
| |
| help: use `get_or_insert(..)` instead: `get_or_insert(2)`

error: unnecessary closure used to substitute value for `Option::None`
--> $DIR/unnecessary_lazy_eval.rs:61:13
--> $DIR/unnecessary_lazy_eval.rs:69:13
|
LL | let _ = deep.0.ok_or_else(|| 2);
| ^^^^^^^----------------
| |
| help: use `ok_or(..)` instead: `ok_or(2)`

error: unnecessary closure used to substitute value for `Option::None`
--> $DIR/unnecessary_lazy_eval.rs:81:28
--> $DIR/unnecessary_lazy_eval.rs:92:28
|
LL | let _: Option<usize> = None.or_else(|| Some(3));
| ^^^^^-------------------
| |
| help: use `or(..)` instead: `or(Some(3))`

error: unnecessary closure used to substitute value for `Option::None`
--> $DIR/unnecessary_lazy_eval.rs:82:13
--> $DIR/unnecessary_lazy_eval.rs:93:13
|
LL | let _ = deep.0.or_else(|| Some(3));
| ^^^^^^^-------------------
| |
| help: use `or(..)` instead: `or(Some(3))`

error: unnecessary closure used to substitute value for `Option::None`
--> $DIR/unnecessary_lazy_eval.rs:83:13
--> $DIR/unnecessary_lazy_eval.rs:94:13
|
LL | let _ = opt.or_else(|| Some(3));
| ^^^^-------------------
| |
| help: use `or(..)` instead: `or(Some(3))`

error: unnecessary closure used to substitute value for `Result::Err`
--> $DIR/unnecessary_lazy_eval.rs:89:13
--> $DIR/unnecessary_lazy_eval.rs:100:13
|
LL | let _ = res2.unwrap_or_else(|_| 2);
| ^^^^^---------------------
| |
| help: use `unwrap_or(..)` instead: `unwrap_or(2)`

error: unnecessary closure used to substitute value for `Result::Err`
--> $DIR/unnecessary_lazy_eval.rs:90:13
--> $DIR/unnecessary_lazy_eval.rs:101:13
|
LL | let _ = res2.unwrap_or_else(|_| astronomers_pi);
| ^^^^^----------------------------------
| |
| help: use `unwrap_or(..)` instead: `unwrap_or(astronomers_pi)`

error: unnecessary closure used to substitute value for `Result::Err`
--> $DIR/unnecessary_lazy_eval.rs:91:13
--> $DIR/unnecessary_lazy_eval.rs:102:13
|
LL | let _ = res2.unwrap_or_else(|_| ext_str.some_field);
| ^^^^^--------------------------------------
| |
| help: use `unwrap_or(..)` instead: `unwrap_or(ext_str.some_field)`

error: unnecessary closure used to substitute value for `Result::Err`
--> $DIR/unnecessary_lazy_eval.rs:113:35
--> $DIR/unnecessary_lazy_eval.rs:124:35
|
LL | let _: Result<usize, usize> = res.and_then(|_| Err(2));
| ^^^^--------------------
| |
| help: use `and(..)` instead: `and(Err(2))`

error: unnecessary closure used to substitute value for `Result::Err`
--> $DIR/unnecessary_lazy_eval.rs:114:35
--> $DIR/unnecessary_lazy_eval.rs:125:35
|
LL | let _: Result<usize, usize> = res.and_then(|_| Err(astronomers_pi));
| ^^^^---------------------------------
| |
| help: use `and(..)` instead: `and(Err(astronomers_pi))`

error: unnecessary closure used to substitute value for `Result::Err`
--> $DIR/unnecessary_lazy_eval.rs:115:35
--> $DIR/unnecessary_lazy_eval.rs:126:35
|
LL | let _: Result<usize, usize> = res.and_then(|_| Err(ext_str.some_field));
| ^^^^-------------------------------------
| |
| help: use `and(..)` instead: `and(Err(ext_str.some_field))`

error: unnecessary closure used to substitute value for `Result::Err`
--> $DIR/unnecessary_lazy_eval.rs:117:35
--> $DIR/unnecessary_lazy_eval.rs:128:35
|
LL | let _: Result<usize, usize> = res.or_else(|_| Ok(2));
| ^^^^------------------
| |
| help: use `or(..)` instead: `or(Ok(2))`

error: unnecessary closure used to substitute value for `Result::Err`
--> $DIR/unnecessary_lazy_eval.rs:118:35
--> $DIR/unnecessary_lazy_eval.rs:129:35
|
LL | let _: Result<usize, usize> = res.or_else(|_| Ok(astronomers_pi));
| ^^^^-------------------------------
| |
| help: use `or(..)` instead: `or(Ok(astronomers_pi))`

error: unnecessary closure used to substitute value for `Result::Err`
--> $DIR/unnecessary_lazy_eval.rs:119:35
--> $DIR/unnecessary_lazy_eval.rs:130:35
|
LL | let _: Result<usize, usize> = res.or_else(|_| Ok(ext_str.some_field));
| ^^^^-----------------------------------
| |
| help: use `or(..)` instead: `or(Ok(ext_str.some_field))`

error: unnecessary closure used to substitute value for `Result::Err`
--> $DIR/unnecessary_lazy_eval.rs:120:35
--> $DIR/unnecessary_lazy_eval.rs:131:35
|
LL | let _: Result<usize, usize> = res.
| ___________________________________^
Expand Down

0 comments on commit 5e8a4b6

Please sign in to comment.