mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-27 07:00:55 +00:00
extend or_fun_call lint to cover methods
This commit is contained in:
parent
57e056dfc7
commit
b764b2a7aa
2 changed files with 18 additions and 7 deletions
|
@ -724,7 +724,7 @@ fn lint_or_fun_call(cx: &LateContext, expr: &hir::Expr, name: &str, args: &[hir:
|
|||
fn check_general_case(
|
||||
cx: &LateContext,
|
||||
name: &str,
|
||||
fun: &hir::Expr,
|
||||
fun_span: Span,
|
||||
self_expr: &hir::Expr,
|
||||
arg: &hir::Expr,
|
||||
or_has_args: bool,
|
||||
|
@ -765,7 +765,7 @@ fn lint_or_fun_call(cx: &LateContext, expr: &hir::Expr, name: &str, args: &[hir:
|
|||
let sugg: Cow<_> = match (fn_has_arguments, !or_has_args) {
|
||||
(true, _) => format!("|_| {}", snippet(cx, arg.span, "..")).into(),
|
||||
(false, false) => format!("|| {}", snippet(cx, arg.span, "..")).into(),
|
||||
(false, true) => snippet(cx, fun.span, ".."),
|
||||
(false, true) => snippet(cx, fun_span, ".."),
|
||||
};
|
||||
|
||||
span_lint_and_then(cx,
|
||||
|
@ -780,11 +780,17 @@ fn lint_or_fun_call(cx: &LateContext, expr: &hir::Expr, name: &str, args: &[hir:
|
|||
}
|
||||
|
||||
if args.len() == 2 {
|
||||
if let hir::ExprCall(ref fun, ref or_args) = args[1].node {
|
||||
let or_has_args = !or_args.is_empty();
|
||||
if !check_unwrap_or_default(cx, name, fun, &args[0], &args[1], or_has_args, expr.span) {
|
||||
check_general_case(cx, name, fun, &args[0], &args[1], or_has_args, expr.span);
|
||||
match args[1].node {
|
||||
hir::ExprCall(ref fun, ref or_args) => {
|
||||
let or_has_args = !or_args.is_empty();
|
||||
if !check_unwrap_or_default(cx, name, fun, &args[0], &args[1], or_has_args, expr.span) {
|
||||
check_general_case(cx, name, fun.span, &args[0], &args[1], or_has_args, expr.span);
|
||||
}
|
||||
}
|
||||
hir::ExprMethodCall(fun, _, ref or_args) => {
|
||||
check_general_case(cx, name, fun.span, &args[0], &args[1], !or_args.is_empty(), expr.span)
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -127,7 +127,6 @@ fn option_methods() {
|
|||
);
|
||||
// macro case
|
||||
let _ = opt_map!(opt, |x| x + 1).unwrap_or_else(|| 0); // should not lint
|
||||
|
||||
}
|
||||
|
||||
/// Struct to generate false positives for things with .iter()
|
||||
|
@ -340,6 +339,12 @@ fn or_fun_call() {
|
|||
//~^ERROR use of `or_insert` followed by a function call
|
||||
//~|HELP try this
|
||||
//~|SUGGESTION btree.entry(42).or_insert_with(String::new);
|
||||
|
||||
let stringy = Some(String::from(""));
|
||||
let _ = stringy.unwrap_or("".to_owned());
|
||||
//~^ERROR use of `unwrap_or`
|
||||
//~|HELP try this
|
||||
//~|SUGGESTION stringy.unwrap_or_else(|| "".to_owned());
|
||||
}
|
||||
|
||||
/// Checks implementation of `ITER_NTH` lint
|
||||
|
|
Loading…
Reference in a new issue