mirror of
https://github.com/rust-lang/rust-clippy
synced 2025-02-24 19:37:20 +00:00
Implement new lint
This commit is contained in:
parent
3bd98895f1
commit
a3ea65c2d9
7 changed files with 494 additions and 1 deletions
|
@ -1646,6 +1646,7 @@ Released 2018-09-13
|
||||||
[`option_map_or_none`]: https://rust-lang.github.io/rust-clippy/master/index.html#option_map_or_none
|
[`option_map_or_none`]: https://rust-lang.github.io/rust-clippy/master/index.html#option_map_or_none
|
||||||
[`option_map_unit_fn`]: https://rust-lang.github.io/rust-clippy/master/index.html#option_map_unit_fn
|
[`option_map_unit_fn`]: https://rust-lang.github.io/rust-clippy/master/index.html#option_map_unit_fn
|
||||||
[`option_option`]: https://rust-lang.github.io/rust-clippy/master/index.html#option_option
|
[`option_option`]: https://rust-lang.github.io/rust-clippy/master/index.html#option_option
|
||||||
|
[`option_unwrap_or_else`]: https://rust-lang.github.io/rust-clippy/master/index.html#option_unwrap_or_else
|
||||||
[`or_fun_call`]: https://rust-lang.github.io/rust-clippy/master/index.html#or_fun_call
|
[`or_fun_call`]: https://rust-lang.github.io/rust-clippy/master/index.html#or_fun_call
|
||||||
[`out_of_bounds_indexing`]: https://rust-lang.github.io/rust-clippy/master/index.html#out_of_bounds_indexing
|
[`out_of_bounds_indexing`]: https://rust-lang.github.io/rust-clippy/master/index.html#out_of_bounds_indexing
|
||||||
[`overflow_check_conditional`]: https://rust-lang.github.io/rust-clippy/master/index.html#overflow_check_conditional
|
[`overflow_check_conditional`]: https://rust-lang.github.io/rust-clippy/master/index.html#overflow_check_conditional
|
||||||
|
|
|
@ -672,6 +672,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
|
||||||
&methods::OK_EXPECT,
|
&methods::OK_EXPECT,
|
||||||
&methods::OPTION_AS_REF_DEREF,
|
&methods::OPTION_AS_REF_DEREF,
|
||||||
&methods::OPTION_MAP_OR_NONE,
|
&methods::OPTION_MAP_OR_NONE,
|
||||||
|
&methods::UNNECESSARY_LAZY_EVALUATION,
|
||||||
&methods::OR_FUN_CALL,
|
&methods::OR_FUN_CALL,
|
||||||
&methods::RESULT_MAP_OR_INTO_OPTION,
|
&methods::RESULT_MAP_OR_INTO_OPTION,
|
||||||
&methods::SEARCH_IS_SOME,
|
&methods::SEARCH_IS_SOME,
|
||||||
|
@ -1360,6 +1361,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
|
||||||
LintId::of(&methods::UNINIT_ASSUMED_INIT),
|
LintId::of(&methods::UNINIT_ASSUMED_INIT),
|
||||||
LintId::of(&methods::UNNECESSARY_FILTER_MAP),
|
LintId::of(&methods::UNNECESSARY_FILTER_MAP),
|
||||||
LintId::of(&methods::UNNECESSARY_FOLD),
|
LintId::of(&methods::UNNECESSARY_FOLD),
|
||||||
|
LintId::of(&methods::UNNECESSARY_LAZY_EVALUATION),
|
||||||
LintId::of(&methods::USELESS_ASREF),
|
LintId::of(&methods::USELESS_ASREF),
|
||||||
LintId::of(&methods::WRONG_SELF_CONVENTION),
|
LintId::of(&methods::WRONG_SELF_CONVENTION),
|
||||||
LintId::of(&methods::ZST_OFFSET),
|
LintId::of(&methods::ZST_OFFSET),
|
||||||
|
@ -1610,6 +1612,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
|
||||||
LintId::of(&methods::FILTER_NEXT),
|
LintId::of(&methods::FILTER_NEXT),
|
||||||
LintId::of(&methods::FLAT_MAP_IDENTITY),
|
LintId::of(&methods::FLAT_MAP_IDENTITY),
|
||||||
LintId::of(&methods::OPTION_AS_REF_DEREF),
|
LintId::of(&methods::OPTION_AS_REF_DEREF),
|
||||||
|
LintId::of(&methods::UNNECESSARY_LAZY_EVALUATION),
|
||||||
LintId::of(&methods::SEARCH_IS_SOME),
|
LintId::of(&methods::SEARCH_IS_SOME),
|
||||||
LintId::of(&methods::SKIP_WHILE_NEXT),
|
LintId::of(&methods::SKIP_WHILE_NEXT),
|
||||||
LintId::of(&methods::SUSPICIOUS_MAP),
|
LintId::of(&methods::SUSPICIOUS_MAP),
|
||||||
|
|
|
@ -1329,6 +1329,32 @@ declare_clippy_lint! {
|
||||||
"`push_str()` used with a single-character string literal as parameter"
|
"`push_str()` used with a single-character string literal as parameter"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
declare_clippy_lint! {
|
||||||
|
/// **What it does:** Looks for unnecessary lazily evaluated closures on `Option` and `Result`.
|
||||||
|
///
|
||||||
|
/// **Why is this bad?** Using eager evaluation is shorter and simpler in some cases.
|
||||||
|
///
|
||||||
|
/// **Known problems:** None.
|
||||||
|
///
|
||||||
|
/// **Example:**
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// // example code where clippy issues a warning
|
||||||
|
/// let opt: Option<u32> = None;
|
||||||
|
///
|
||||||
|
/// opt.unwrap_or_else(|| 42);
|
||||||
|
/// ```
|
||||||
|
/// Use instead:
|
||||||
|
/// ```rust
|
||||||
|
/// let opt: Option<u32> = None;
|
||||||
|
///
|
||||||
|
/// opt.unwrap_or(42);
|
||||||
|
/// ```
|
||||||
|
pub UNNECESSARY_LAZY_EVALUATION,
|
||||||
|
style,
|
||||||
|
"using unnecessary lazy evaluation, which can be replaced with simpler eager evaluation"
|
||||||
|
}
|
||||||
|
|
||||||
declare_lint_pass!(Methods => [
|
declare_lint_pass!(Methods => [
|
||||||
UNWRAP_USED,
|
UNWRAP_USED,
|
||||||
EXPECT_USED,
|
EXPECT_USED,
|
||||||
|
@ -1378,6 +1404,7 @@ declare_lint_pass!(Methods => [
|
||||||
ZST_OFFSET,
|
ZST_OFFSET,
|
||||||
FILETYPE_IS_FILE,
|
FILETYPE_IS_FILE,
|
||||||
OPTION_AS_REF_DEREF,
|
OPTION_AS_REF_DEREF,
|
||||||
|
UNNECESSARY_LAZY_EVALUATION,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
impl<'tcx> LateLintPass<'tcx> for Methods {
|
impl<'tcx> LateLintPass<'tcx> for Methods {
|
||||||
|
@ -1398,13 +1425,18 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
|
||||||
["expect", "ok"] => lint_ok_expect(cx, expr, arg_lists[1]),
|
["expect", "ok"] => lint_ok_expect(cx, expr, arg_lists[1]),
|
||||||
["expect", ..] => lint_expect(cx, expr, arg_lists[0]),
|
["expect", ..] => lint_expect(cx, expr, arg_lists[0]),
|
||||||
["unwrap_or", "map"] => option_map_unwrap_or::lint(cx, expr, arg_lists[1], arg_lists[0], method_spans[1]),
|
["unwrap_or", "map"] => option_map_unwrap_or::lint(cx, expr, arg_lists[1], arg_lists[0], method_spans[1]),
|
||||||
["unwrap_or_else", "map"] => lint_map_unwrap_or_else(cx, expr, arg_lists[1], arg_lists[0]),
|
["unwrap_or_else", "map"] => {
|
||||||
|
lint_lazy_eval(cx, expr, arg_lists[0], true, "unwrap_or");
|
||||||
|
lint_map_unwrap_or_else(cx, expr, arg_lists[1], arg_lists[0]);
|
||||||
|
},
|
||||||
["map_or", ..] => lint_map_or_none(cx, expr, arg_lists[0]),
|
["map_or", ..] => lint_map_or_none(cx, expr, arg_lists[0]),
|
||||||
["and_then", ..] => {
|
["and_then", ..] => {
|
||||||
|
lint_lazy_eval(cx, expr, arg_lists[0], false, "and");
|
||||||
bind_instead_of_map::OptionAndThenSome::lint(cx, expr, arg_lists[0]);
|
bind_instead_of_map::OptionAndThenSome::lint(cx, expr, arg_lists[0]);
|
||||||
bind_instead_of_map::ResultAndThenOk::lint(cx, expr, arg_lists[0]);
|
bind_instead_of_map::ResultAndThenOk::lint(cx, expr, arg_lists[0]);
|
||||||
},
|
},
|
||||||
["or_else", ..] => {
|
["or_else", ..] => {
|
||||||
|
lint_lazy_eval(cx, expr, arg_lists[0], false, "or");
|
||||||
bind_instead_of_map::ResultOrElseErrInfo::lint(cx, expr, arg_lists[0]);
|
bind_instead_of_map::ResultOrElseErrInfo::lint(cx, expr, arg_lists[0]);
|
||||||
},
|
},
|
||||||
["next", "filter"] => lint_filter_next(cx, expr, arg_lists[1]),
|
["next", "filter"] => lint_filter_next(cx, expr, arg_lists[1]),
|
||||||
|
@ -1448,6 +1480,9 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
|
||||||
["is_file", ..] => lint_filetype_is_file(cx, expr, arg_lists[0]),
|
["is_file", ..] => lint_filetype_is_file(cx, expr, arg_lists[0]),
|
||||||
["map", "as_ref"] => lint_option_as_ref_deref(cx, expr, arg_lists[1], arg_lists[0], false),
|
["map", "as_ref"] => lint_option_as_ref_deref(cx, expr, arg_lists[1], arg_lists[0], false),
|
||||||
["map", "as_mut"] => lint_option_as_ref_deref(cx, expr, arg_lists[1], arg_lists[0], true),
|
["map", "as_mut"] => lint_option_as_ref_deref(cx, expr, arg_lists[1], arg_lists[0], true),
|
||||||
|
["unwrap_or_else", ..] => lint_lazy_eval(cx, expr, arg_lists[0], true, "unwrap_or"),
|
||||||
|
["get_or_insert_with", ..] => lint_lazy_eval(cx, expr, arg_lists[0], true, "get_or_insert"),
|
||||||
|
["ok_or_else", ..] => lint_lazy_eval(cx, expr, arg_lists[0], true, "ok_or"),
|
||||||
_ => {},
|
_ => {},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2663,6 +2698,99 @@ fn lint_map_flatten<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>, map
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// lint use of `<fn>_else(simple closure)` for `Option`s and `Result`s that can be
|
||||||
|
/// replaced with `<fn>(return value of simple closure)`
|
||||||
|
fn lint_lazy_eval<'a, 'tcx>(
|
||||||
|
cx: &LateContext<'a, 'tcx>,
|
||||||
|
expr: &'tcx hir::Expr<'_>,
|
||||||
|
args: &'tcx [hir::Expr<'_>],
|
||||||
|
allow_variant_calls: bool,
|
||||||
|
simplify_using: &str,
|
||||||
|
) {
|
||||||
|
let is_option = is_type_diagnostic_item(cx, cx.tables.expr_ty(&args[0]), sym!(option_type));
|
||||||
|
let is_result = is_type_diagnostic_item(cx, cx.tables.expr_ty(&args[0]), sym!(result_type));
|
||||||
|
|
||||||
|
if !is_option && !is_result {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return true if the expression is an accessor of any of the arguments
|
||||||
|
fn expr_uses_argument(expr: &hir::Expr<'_>, params: &[hir::Param<'_>]) -> bool {
|
||||||
|
params.iter().any(|arg| {
|
||||||
|
if_chain! {
|
||||||
|
if let hir::PatKind::Binding(_, _, ident, _) = arg.pat.kind;
|
||||||
|
if let hir::ExprKind::Path(hir::QPath::Resolved(_, ref path)) = expr.kind;
|
||||||
|
if let [p, ..] = path.segments;
|
||||||
|
then {
|
||||||
|
ident.name == p.ident.name
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn match_any_qpath(path: &hir::QPath<'_>, paths: &[&[&str]]) -> bool {
|
||||||
|
paths.iter().any(|candidate| match_qpath(path, candidate))
|
||||||
|
}
|
||||||
|
|
||||||
|
if let hir::ExprKind::Closure(_, _, eid, _, _) = args[1].kind {
|
||||||
|
let body = cx.tcx.hir().body(eid);
|
||||||
|
let ex = &body.value;
|
||||||
|
let params = &body.params;
|
||||||
|
|
||||||
|
let simplify = match ex.kind {
|
||||||
|
// Closures returning literals can be unconditionally simplified
|
||||||
|
hir::ExprKind::Lit(_) => true,
|
||||||
|
|
||||||
|
// Reading fields can be simplified if the object is not an argument of the closure
|
||||||
|
hir::ExprKind::Field(ref object, _) => !expr_uses_argument(object, params),
|
||||||
|
|
||||||
|
// Paths can be simplified if the root is not the argument, this also covers None
|
||||||
|
hir::ExprKind::Path(_) => !expr_uses_argument(ex, params),
|
||||||
|
|
||||||
|
// Calls to Some, Ok, Err can be considered literals if they don't derive an argument
|
||||||
|
hir::ExprKind::Call(ref func, ref args) => if_chain! {
|
||||||
|
if allow_variant_calls; // Disable lint when rules conflict with bind_instead_of_map
|
||||||
|
if let hir::ExprKind::Path(ref path) = func.kind;
|
||||||
|
if match_any_qpath(path, &[&["Some"], &["Ok"], &["Err"]]);
|
||||||
|
then {
|
||||||
|
!args.iter().any(|arg| expr_uses_argument(arg, params))
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
// For anything more complex than the above, a closure is probably the right solution,
|
||||||
|
// or the case is handled by an other lint
|
||||||
|
_ => false,
|
||||||
|
};
|
||||||
|
|
||||||
|
if simplify {
|
||||||
|
let msg = if is_option {
|
||||||
|
"unnecessary closure used to substitute value for `Option::None`"
|
||||||
|
} else {
|
||||||
|
"unnecessary closure used to substitute value for `Result::Err`"
|
||||||
|
};
|
||||||
|
|
||||||
|
span_lint_and_sugg(
|
||||||
|
cx,
|
||||||
|
UNNECESSARY_LAZY_EVALUATION,
|
||||||
|
expr.span,
|
||||||
|
msg,
|
||||||
|
&format!("Use `{}` instead", simplify_using),
|
||||||
|
format!(
|
||||||
|
"{0}.{1}({2})",
|
||||||
|
snippet(cx, args[0].span, ".."),
|
||||||
|
simplify_using,
|
||||||
|
snippet(cx, ex.span, ".."),
|
||||||
|
),
|
||||||
|
Applicability::MachineApplicable,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// lint use of `map().unwrap_or_else()` for `Option`s and `Result`s
|
/// lint use of `map().unwrap_or_else()` for `Option`s and `Result`s
|
||||||
fn lint_map_unwrap_or_else<'tcx>(
|
fn lint_map_unwrap_or_else<'tcx>(
|
||||||
cx: &LateContext<'tcx>,
|
cx: &LateContext<'tcx>,
|
||||||
|
|
|
@ -2383,6 +2383,13 @@ pub static ref ALL_LINTS: Vec<Lint> = vec![
|
||||||
deprecation: None,
|
deprecation: None,
|
||||||
module: "methods",
|
module: "methods",
|
||||||
},
|
},
|
||||||
|
Lint {
|
||||||
|
name: "unnecessary_lazy_eval",
|
||||||
|
group: "style",
|
||||||
|
desc: "using unnecessary lazy evaluation, which can be replaced with simpler eager evaluation",
|
||||||
|
deprecation: None,
|
||||||
|
module: "methods",
|
||||||
|
},
|
||||||
Lint {
|
Lint {
|
||||||
name: "unnecessary_mut_passed",
|
name: "unnecessary_mut_passed",
|
||||||
group: "style",
|
group: "style",
|
||||||
|
|
105
tests/ui/unnecessary_lazy_eval.fixed
Normal file
105
tests/ui/unnecessary_lazy_eval.fixed
Normal file
|
@ -0,0 +1,105 @@
|
||||||
|
// run-rustfix
|
||||||
|
#![warn(clippy::unnecessary_lazy_evaluation)]
|
||||||
|
#![allow(clippy::redundant_closure)]
|
||||||
|
#![allow(clippy::bind_instead_of_map)]
|
||||||
|
|
||||||
|
struct Deep(Option<u32>);
|
||||||
|
|
||||||
|
#[derive(Copy, Clone)]
|
||||||
|
struct SomeStruct {
|
||||||
|
some_field: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SomeStruct {
|
||||||
|
fn return_some_field(&self) -> u32 {
|
||||||
|
self.some_field
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn some_call<T: Default>() -> T {
|
||||||
|
T::default()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let astronomers_pi = 10;
|
||||||
|
let ext_str = SomeStruct { some_field: 10 };
|
||||||
|
|
||||||
|
// Should lint - Option
|
||||||
|
let mut opt = Some(42);
|
||||||
|
let ext_opt = Some(42);
|
||||||
|
let _ = opt.unwrap_or(2);
|
||||||
|
let _ = opt.unwrap_or(astronomers_pi);
|
||||||
|
let _ = opt.unwrap_or(ext_str.some_field);
|
||||||
|
let _ = opt.and(ext_opt);
|
||||||
|
let _ = opt.or(ext_opt);
|
||||||
|
let _ = opt.or(None);
|
||||||
|
let _ = opt.get_or_insert(2);
|
||||||
|
let _ = opt.ok_or(2);
|
||||||
|
|
||||||
|
// Cases when unwrap is not called on a simple variable
|
||||||
|
let _ = Some(10).unwrap_or(2);
|
||||||
|
let _ = Some(10).and(ext_opt);
|
||||||
|
let _: Option<u32> = None.or(ext_opt);
|
||||||
|
let _ = None.get_or_insert(2);
|
||||||
|
let _: Result<u32, u32> = None.ok_or(2);
|
||||||
|
let _: Option<u32> = None.or(None);
|
||||||
|
|
||||||
|
let mut deep = Deep(Some(42));
|
||||||
|
let _ = deep.0.unwrap_or(2);
|
||||||
|
let _ = deep.0.and(ext_opt);
|
||||||
|
let _ = deep.0.or(None);
|
||||||
|
let _ = deep.0.get_or_insert(2);
|
||||||
|
let _ = deep.0.ok_or(2);
|
||||||
|
|
||||||
|
// Should not lint - Option
|
||||||
|
let _ = opt.unwrap_or_else(|| ext_str.return_some_field());
|
||||||
|
let _ = opt.or_else(some_call);
|
||||||
|
let _ = opt.or_else(|| some_call());
|
||||||
|
let _: Result<u32, u32> = opt.ok_or_else(|| some_call());
|
||||||
|
let _: Result<u32, u32> = opt.ok_or_else(some_call);
|
||||||
|
let _ = deep.0.get_or_insert_with(|| some_call());
|
||||||
|
let _ = deep.0.or_else(some_call);
|
||||||
|
let _ = deep.0.or_else(|| some_call());
|
||||||
|
|
||||||
|
// These are handled by bind_instead_of_map
|
||||||
|
let _: Option<u32> = None.or_else(|| Some(3));
|
||||||
|
let _ = deep.0.or_else(|| Some(3));
|
||||||
|
let _ = opt.or_else(|| Some(3));
|
||||||
|
|
||||||
|
// Should lint - Result
|
||||||
|
let res: Result<u32, u32> = Err(5);
|
||||||
|
let res2: Result<u32, SomeStruct> = Err(SomeStruct { some_field: 5 });
|
||||||
|
|
||||||
|
let _ = res2.unwrap_or(2);
|
||||||
|
let _ = res2.unwrap_or(astronomers_pi);
|
||||||
|
let _ = res2.unwrap_or(ext_str.some_field);
|
||||||
|
|
||||||
|
// Should not lint - Result
|
||||||
|
let _ = res.unwrap_or_else(|err| err);
|
||||||
|
let _ = res2.unwrap_or_else(|err| err.some_field);
|
||||||
|
let _ = res2.unwrap_or_else(|err| err.return_some_field());
|
||||||
|
let _ = res2.unwrap_or_else(|_| ext_str.return_some_field());
|
||||||
|
|
||||||
|
let _: Result<u32, u32> = res.and_then(|x| Ok(x));
|
||||||
|
let _: Result<u32, u32> = res.and_then(|x| Err(x));
|
||||||
|
|
||||||
|
let _: Result<u32, u32> = res.or_else(|err| Ok(err));
|
||||||
|
let _: Result<u32, u32> = res.or_else(|err| Err(err));
|
||||||
|
|
||||||
|
// These are handled by bind_instead_of_map
|
||||||
|
let _: Result<u32, u32> = res.and_then(|_| Ok(2));
|
||||||
|
let _: Result<u32, u32> = res.and_then(|_| Ok(astronomers_pi));
|
||||||
|
let _: Result<u32, u32> = res.and_then(|_| Ok(ext_str.some_field));
|
||||||
|
|
||||||
|
let _: Result<u32, u32> = res.and_then(|_| Err(2));
|
||||||
|
let _: Result<u32, u32> = res.and_then(|_| Err(astronomers_pi));
|
||||||
|
let _: Result<u32, u32> = res.and_then(|_| Err(ext_str.some_field));
|
||||||
|
|
||||||
|
let _: Result<u32, u32> = res.or_else(|_| Ok(2));
|
||||||
|
let _: Result<u32, u32> = res.or_else(|_| Ok(astronomers_pi));
|
||||||
|
let _: Result<u32, u32> = res.or_else(|_| Ok(ext_str.some_field));
|
||||||
|
|
||||||
|
let _: Result<u32, u32> = res.or_else(|_| Err(2));
|
||||||
|
let _: Result<u32, u32> = res.or_else(|_| Err(astronomers_pi));
|
||||||
|
let _: Result<u32, u32> = res.or_else(|_| Err(ext_str.some_field));
|
||||||
|
}
|
105
tests/ui/unnecessary_lazy_eval.rs
Normal file
105
tests/ui/unnecessary_lazy_eval.rs
Normal file
|
@ -0,0 +1,105 @@
|
||||||
|
// run-rustfix
|
||||||
|
#![warn(clippy::unnecessary_lazy_eval)]
|
||||||
|
#![allow(clippy::redundant_closure)]
|
||||||
|
#![allow(clippy::bind_instead_of_map)]
|
||||||
|
|
||||||
|
struct Deep(Option<u32>);
|
||||||
|
|
||||||
|
#[derive(Copy, Clone)]
|
||||||
|
struct SomeStruct {
|
||||||
|
some_field: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SomeStruct {
|
||||||
|
fn return_some_field(&self) -> u32 {
|
||||||
|
self.some_field
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn some_call<T: Default>() -> T {
|
||||||
|
T::default()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let astronomers_pi = 10;
|
||||||
|
let ext_str = SomeStruct { some_field: 10 };
|
||||||
|
|
||||||
|
// Should lint - Option
|
||||||
|
let mut opt = Some(42);
|
||||||
|
let ext_opt = Some(42);
|
||||||
|
let _ = opt.unwrap_or_else(|| 2);
|
||||||
|
let _ = opt.unwrap_or_else(|| astronomers_pi);
|
||||||
|
let _ = opt.unwrap_or_else(|| ext_str.some_field);
|
||||||
|
let _ = opt.and_then(|_| ext_opt);
|
||||||
|
let _ = opt.or_else(|| ext_opt);
|
||||||
|
let _ = opt.or_else(|| None);
|
||||||
|
let _ = opt.get_or_insert_with(|| 2);
|
||||||
|
let _ = opt.ok_or_else(|| 2);
|
||||||
|
|
||||||
|
// Cases when unwrap is not called on a simple variable
|
||||||
|
let _ = Some(10).unwrap_or_else(|| 2);
|
||||||
|
let _ = Some(10).and_then(|_| ext_opt);
|
||||||
|
let _: Option<u32> = None.or_else(|| ext_opt);
|
||||||
|
let _ = None.get_or_insert_with(|| 2);
|
||||||
|
let _: Result<u32, u32> = None.ok_or_else(|| 2);
|
||||||
|
let _: Option<u32> = None.or_else(|| None);
|
||||||
|
|
||||||
|
let mut deep = Deep(Some(42));
|
||||||
|
let _ = deep.0.unwrap_or_else(|| 2);
|
||||||
|
let _ = deep.0.and_then(|_| ext_opt);
|
||||||
|
let _ = deep.0.or_else(|| None);
|
||||||
|
let _ = deep.0.get_or_insert_with(|| 2);
|
||||||
|
let _ = deep.0.ok_or_else(|| 2);
|
||||||
|
|
||||||
|
// Should not lint - Option
|
||||||
|
let _ = opt.unwrap_or_else(|| ext_str.return_some_field());
|
||||||
|
let _ = opt.or_else(some_call);
|
||||||
|
let _ = opt.or_else(|| some_call());
|
||||||
|
let _: Result<u32, u32> = opt.ok_or_else(|| some_call());
|
||||||
|
let _: Result<u32, u32> = opt.ok_or_else(some_call);
|
||||||
|
let _ = deep.0.get_or_insert_with(|| some_call());
|
||||||
|
let _ = deep.0.or_else(some_call);
|
||||||
|
let _ = deep.0.or_else(|| some_call());
|
||||||
|
|
||||||
|
// These are handled by bind_instead_of_map
|
||||||
|
let _: Option<u32> = None.or_else(|| Some(3));
|
||||||
|
let _ = deep.0.or_else(|| Some(3));
|
||||||
|
let _ = opt.or_else(|| Some(3));
|
||||||
|
|
||||||
|
// Should lint - Result
|
||||||
|
let res: Result<u32, u32> = Err(5);
|
||||||
|
let res2: Result<u32, SomeStruct> = Err(SomeStruct { some_field: 5 });
|
||||||
|
|
||||||
|
let _ = res2.unwrap_or_else(|_| 2);
|
||||||
|
let _ = res2.unwrap_or_else(|_| astronomers_pi);
|
||||||
|
let _ = res2.unwrap_or_else(|_| ext_str.some_field);
|
||||||
|
|
||||||
|
// Should not lint - Result
|
||||||
|
let _ = res.unwrap_or_else(|err| err);
|
||||||
|
let _ = res2.unwrap_or_else(|err| err.some_field);
|
||||||
|
let _ = res2.unwrap_or_else(|err| err.return_some_field());
|
||||||
|
let _ = res2.unwrap_or_else(|_| ext_str.return_some_field());
|
||||||
|
|
||||||
|
let _: Result<u32, u32> = res.and_then(|x| Ok(x));
|
||||||
|
let _: Result<u32, u32> = res.and_then(|x| Err(x));
|
||||||
|
|
||||||
|
let _: Result<u32, u32> = res.or_else(|err| Ok(err));
|
||||||
|
let _: Result<u32, u32> = res.or_else(|err| Err(err));
|
||||||
|
|
||||||
|
// These are handled by bind_instead_of_map
|
||||||
|
let _: Result<u32, u32> = res.and_then(|_| Ok(2));
|
||||||
|
let _: Result<u32, u32> = res.and_then(|_| Ok(astronomers_pi));
|
||||||
|
let _: Result<u32, u32> = res.and_then(|_| Ok(ext_str.some_field));
|
||||||
|
|
||||||
|
let _: Result<u32, u32> = res.and_then(|_| Err(2));
|
||||||
|
let _: Result<u32, u32> = res.and_then(|_| Err(astronomers_pi));
|
||||||
|
let _: Result<u32, u32> = res.and_then(|_| Err(ext_str.some_field));
|
||||||
|
|
||||||
|
let _: Result<u32, u32> = res.or_else(|_| Ok(2));
|
||||||
|
let _: Result<u32, u32> = res.or_else(|_| Ok(astronomers_pi));
|
||||||
|
let _: Result<u32, u32> = res.or_else(|_| Ok(ext_str.some_field));
|
||||||
|
|
||||||
|
let _: Result<u32, u32> = res.or_else(|_| Err(2));
|
||||||
|
let _: Result<u32, u32> = res.or_else(|_| Err(astronomers_pi));
|
||||||
|
let _: Result<u32, u32> = res.or_else(|_| Err(ext_str.some_field));
|
||||||
|
}
|
144
tests/ui/unnecessary_lazy_eval.stderr
Normal file
144
tests/ui/unnecessary_lazy_eval.stderr
Normal file
|
@ -0,0 +1,144 @@
|
||||||
|
error: unnecessary closure used to substitute value for `Option::None`
|
||||||
|
--> $DIR/unnecessary_lazy_eval.rs:30:13
|
||||||
|
|
|
||||||
|
LL | let _ = opt.unwrap_or_else(|| 2);
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: Use `unwrap_or` instead: `opt.unwrap_or(2)`
|
||||||
|
|
|
||||||
|
= note: `-D clippy::unnecessary-lazy-evaluation` implied by `-D warnings`
|
||||||
|
|
||||||
|
error: unnecessary closure used to substitute value for `Option::None`
|
||||||
|
--> $DIR/unnecessary_lazy_eval.rs:31:13
|
||||||
|
|
|
||||||
|
LL | let _ = opt.unwrap_or_else(|| astronomers_pi);
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: Use `unwrap_or` instead: `opt.unwrap_or(astronomers_pi)`
|
||||||
|
|
||||||
|
error: unnecessary closure used to substitute value for `Option::None`
|
||||||
|
--> $DIR/unnecessary_lazy_eval.rs:32:13
|
||||||
|
|
|
||||||
|
LL | let _ = opt.unwrap_or_else(|| ext_str.some_field);
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: Use `unwrap_or` instead: `opt.unwrap_or(ext_str.some_field)`
|
||||||
|
|
||||||
|
error: unnecessary closure used to substitute value for `Option::None`
|
||||||
|
--> $DIR/unnecessary_lazy_eval.rs:33:13
|
||||||
|
|
|
||||||
|
LL | let _ = opt.and_then(|_| ext_opt);
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: Use `and` instead: `opt.and(ext_opt)`
|
||||||
|
|
||||||
|
error: unnecessary closure used to substitute value for `Option::None`
|
||||||
|
--> $DIR/unnecessary_lazy_eval.rs:34:13
|
||||||
|
|
|
||||||
|
LL | let _ = opt.or_else(|| ext_opt);
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^ help: Use `or` instead: `opt.or(ext_opt)`
|
||||||
|
|
||||||
|
error: unnecessary closure used to substitute value for `Option::None`
|
||||||
|
--> $DIR/unnecessary_lazy_eval.rs:35:13
|
||||||
|
|
|
||||||
|
LL | let _ = opt.or_else(|| None);
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^ help: Use `or` instead: `opt.or(None)`
|
||||||
|
|
||||||
|
error: unnecessary closure used to substitute value for `Option::None`
|
||||||
|
--> $DIR/unnecessary_lazy_eval.rs:36:13
|
||||||
|
|
|
||||||
|
LL | let _ = opt.get_or_insert_with(|| 2);
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: Use `get_or_insert` instead: `opt.get_or_insert(2)`
|
||||||
|
|
||||||
|
error: unnecessary closure used to substitute value for `Option::None`
|
||||||
|
--> $DIR/unnecessary_lazy_eval.rs:37:13
|
||||||
|
|
|
||||||
|
LL | let _ = opt.ok_or_else(|| 2);
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^ help: Use `ok_or` instead: `opt.ok_or(2)`
|
||||||
|
|
||||||
|
error: unnecessary closure used to substitute value for `Option::None`
|
||||||
|
--> $DIR/unnecessary_lazy_eval.rs:40:13
|
||||||
|
|
|
||||||
|
LL | let _ = Some(10).unwrap_or_else(|| 2);
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: Use `unwrap_or` instead: `Some(10).unwrap_or(2)`
|
||||||
|
|
||||||
|
error: unnecessary closure used to substitute value for `Option::None`
|
||||||
|
--> $DIR/unnecessary_lazy_eval.rs:41:13
|
||||||
|
|
|
||||||
|
LL | let _ = Some(10).and_then(|_| ext_opt);
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: Use `and` instead: `Some(10).and(ext_opt)`
|
||||||
|
|
||||||
|
error: unnecessary closure used to substitute value for `Option::None`
|
||||||
|
--> $DIR/unnecessary_lazy_eval.rs:42:26
|
||||||
|
|
|
||||||
|
LL | let _: Option<u32> = None.or_else(|| ext_opt);
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: Use `or` instead: `None.or(ext_opt)`
|
||||||
|
|
||||||
|
error: unnecessary closure used to substitute value for `Option::None`
|
||||||
|
--> $DIR/unnecessary_lazy_eval.rs:43:13
|
||||||
|
|
|
||||||
|
LL | let _ = None.get_or_insert_with(|| 2);
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: Use `get_or_insert` instead: `None.get_or_insert(2)`
|
||||||
|
|
||||||
|
error: unnecessary closure used to substitute value for `Option::None`
|
||||||
|
--> $DIR/unnecessary_lazy_eval.rs:44:31
|
||||||
|
|
|
||||||
|
LL | let _: Result<u32, u32> = None.ok_or_else(|| 2);
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^ help: Use `ok_or` instead: `None.ok_or(2)`
|
||||||
|
|
||||||
|
error: unnecessary closure used to substitute value for `Option::None`
|
||||||
|
--> $DIR/unnecessary_lazy_eval.rs:45:26
|
||||||
|
|
|
||||||
|
LL | let _: Option<u32> = None.or_else(|| None);
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^ help: Use `or` instead: `None.or(None)`
|
||||||
|
|
||||||
|
error: unnecessary closure used to substitute value for `Option::None`
|
||||||
|
--> $DIR/unnecessary_lazy_eval.rs:48:13
|
||||||
|
|
|
||||||
|
LL | let _ = deep.0.unwrap_or_else(|| 2);
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: Use `unwrap_or` instead: `deep.0.unwrap_or(2)`
|
||||||
|
|
||||||
|
error: unnecessary closure used to substitute value for `Option::None`
|
||||||
|
--> $DIR/unnecessary_lazy_eval.rs:49:13
|
||||||
|
|
|
||||||
|
LL | let _ = deep.0.and_then(|_| ext_opt);
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: Use `and` instead: `deep.0.and(ext_opt)`
|
||||||
|
|
||||||
|
error: unnecessary closure used to substitute value for `Option::None`
|
||||||
|
--> $DIR/unnecessary_lazy_eval.rs:50:13
|
||||||
|
|
|
||||||
|
LL | let _ = deep.0.or_else(|| None);
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^ help: Use `or` instead: `deep.0.or(None)`
|
||||||
|
|
||||||
|
error: unnecessary closure used to substitute value for `Option::None`
|
||||||
|
--> $DIR/unnecessary_lazy_eval.rs:51:13
|
||||||
|
|
|
||||||
|
LL | let _ = deep.0.get_or_insert_with(|| 2);
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: Use `get_or_insert` instead: `deep.0.get_or_insert(2)`
|
||||||
|
|
||||||
|
error: unnecessary closure used to substitute value for `Option::None`
|
||||||
|
--> $DIR/unnecessary_lazy_eval.rs:52:13
|
||||||
|
|
|
||||||
|
LL | let _ = deep.0.ok_or_else(|| 2);
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^ help: Use `ok_or` instead: `deep.0.ok_or(2)`
|
||||||
|
|
||||||
|
error: unnecessary closure used to substitute value for `Result::Err`
|
||||||
|
--> $DIR/unnecessary_lazy_eval.rs:73:13
|
||||||
|
|
|
||||||
|
LL | let _ = res2.unwrap_or_else(|_| 2);
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: Use `unwrap_or` instead: `res2.unwrap_or(2)`
|
||||||
|
|
||||||
|
error: unnecessary closure used to substitute value for `Result::Err`
|
||||||
|
--> $DIR/unnecessary_lazy_eval.rs:74:13
|
||||||
|
|
|
||||||
|
LL | let _ = res2.unwrap_or_else(|_| astronomers_pi);
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: Use `unwrap_or` instead: `res2.unwrap_or(astronomers_pi)`
|
||||||
|
|
||||||
|
error: unnecessary closure used to substitute value for `Result::Err`
|
||||||
|
--> $DIR/unnecessary_lazy_eval.rs:75:13
|
||||||
|
|
|
||||||
|
LL | let _ = res2.unwrap_or_else(|_| ext_str.some_field);
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: Use `unwrap_or` instead: `res2.unwrap_or(ext_str.some_field)`
|
||||||
|
|
||||||
|
error: unknown clippy lint: clippy::unnecessary_lazy_eval
|
||||||
|
--> $DIR/unnecessary_lazy_eval.rs:2:9
|
||||||
|
|
|
||||||
|
LL | #![warn(clippy::unnecessary_lazy_eval)]
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: did you mean: `clippy::unnecessary_lazy_evaluation`
|
||||||
|
|
|
||||||
|
= note: `-D clippy::unknown-clippy-lints` implied by `-D warnings`
|
||||||
|
|
||||||
|
error: aborting due to 23 previous errors
|
||||||
|
|
Loading…
Add table
Reference in a new issue