Skip to content

Commit

Permalink
reduce code repetition for the original lint
Browse files Browse the repository at this point in the history
  • Loading branch information
J-ZhengLi committed Feb 21, 2022
1 parent fb94992 commit 7aed5f0
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 74 deletions.
126 changes: 56 additions & 70 deletions clippy_lints/src/question_mark.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::higher;
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::sugg::Sugg;
use clippy_utils::ty::is_type_diagnostic_item;
use clippy_utils::{eq_expr_value, is_lang_ctor, path_to_local, path_to_local_id, peel_blocks, peel_blocks_with_stmt};
use clippy_utils::{higher, sugg};
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir::LangItem::{OptionNone, OptionSome, ResultOk};
use rustc_hir::{BindingAnnotation, Expr, ExprKind, PatKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty::Ty;
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::sym;
use rustc_span::{sym, symbol::Symbol};

declare_clippy_lint! {
/// ### What it does
Expand Down Expand Up @@ -60,35 +60,25 @@ impl QuestionMark {
if let Some(higher::If { cond, then, r#else }) = higher::If::hir(expr);
if let ExprKind::MethodCall(segment, args, _) = &cond.kind;
if let Some(subject) = args.get(0);
if (Self::option_check_and_early_return(cx, subject, then) && segment.ident.name == sym!(is_none)) ||
(Self::result_check_and_early_return(cx, subject, then) && segment.ident.name == sym!(is_err));
let subject_ty = cx.typeck_results().expr_ty(subject);
if (Self::check_early_return(cx, subject, then, subject_ty, sym::Option) &&
segment.ident.name == sym!(is_none)) ||
(Self::check_early_return(cx, subject, then, subject_ty, sym::Result) &&
segment.ident.name == sym!(is_err));
then {
let mut applicability = Applicability::MachineApplicable;
let receiver_str = &Sugg::hir_with_applicability(cx, subject, "..", &mut applicability);
let mut replacement: Option<String> = None;
let receiver_str = snippet_with_applicability(cx, subject.span, "..", &mut applicability);
let by_ref = !subject_ty.is_copy_modulo_regions(cx.tcx.at(subject.span), cx.param_env) &&
!matches!(subject.kind, ExprKind::Call(..) | ExprKind::MethodCall(..));
let mut suggestion = String::new();
if let Some(else_inner) = r#else {
if eq_expr_value(cx, subject, peel_blocks(else_inner)) {
replacement = Some(format!("Some({}?)", receiver_str));
suggestion = format!("Some({}?)", receiver_str);
}
} else if Self::moves_by_default(cx, subject)
&& !matches!(subject.kind, ExprKind::Call(..) | ExprKind::MethodCall(..))
{
replacement = Some(format!("{}.as_ref()?;", receiver_str));
} else {
replacement = Some(format!("{}?;", receiver_str));
}

if let Some(replacement_str) = replacement {
span_lint_and_sugg(
cx,
QUESTION_MARK,
expr.span,
"this block may be rewritten with the `?` operator",
"replace it with",
replacement_str,
applicability,
);
suggestion = format!("{}{}?;", receiver_str, if by_ref { ".as_ref()" } else { "" });
}
Self::offer_suggestion(cx, expr, suggestion, applicability);
}
}
}
Expand All @@ -98,8 +88,11 @@ impl QuestionMark {
if let Some(higher::IfLet { let_pat, let_expr, if_then, if_else: Some(if_else) })
= higher::IfLet::hir(cx, expr);
if let PatKind::TupleStruct(ref path1, fields, None) = let_pat.kind;
if (Self::option_check_and_early_return(cx, let_expr, if_else) && is_lang_ctor(cx, path1, OptionSome)) ||
(Self::result_check_and_early_return(cx, let_expr, if_else) && is_lang_ctor(cx, path1, ResultOk));
let let_expr_ty = cx.typeck_results().expr_ty(let_expr);
if (Self::check_early_return(cx, let_expr, if_else, let_expr_ty, sym::Option) &&
is_lang_ctor(cx, path1, OptionSome)) ||
(Self::check_early_return(cx, let_expr, if_else, let_expr_ty, sym::Result) &&
is_lang_ctor(cx, path1, ResultOk));

if let PatKind::Binding(annot, bind_id, _, _) = fields[0].kind;
let by_ref = matches!(annot, BindingAnnotation::Ref | BindingAnnotation::RefMut);
Expand All @@ -108,59 +101,52 @@ impl QuestionMark {
let mut applicability = Applicability::MachineApplicable;
let receiver_str = snippet_with_applicability(cx, let_expr.span, "..", &mut applicability);
let replacement = format!("{}{}?", receiver_str, if by_ref { ".as_ref()" } else { "" },);

span_lint_and_sugg(
cx,
QUESTION_MARK,
expr.span,
"this if-let-else may be rewritten with the `?` operator",
"replace it with",
replacement,
applicability,
);
Self::offer_suggestion(cx, expr, replacement, applicability);
}
}
}

fn result_check_and_early_return(cx: &LateContext<'_>, expr: &Expr<'_>, nested_expr: &Expr<'_>) -> bool {
Self::is_result(cx, expr) && Self::expression_returns_unmodified_err(cx, nested_expr, expr)
}

fn option_check_and_early_return(cx: &LateContext<'_>, expr: &Expr<'_>, nested_expr: &Expr<'_>) -> bool {
Self::is_option(cx, expr) && Self::expression_returns_none(cx, nested_expr)
}

fn moves_by_default(cx: &LateContext<'_>, expression: &Expr<'_>) -> bool {
let expr_ty = cx.typeck_results().expr_ty(expression);

!expr_ty.is_copy_modulo_regions(cx.tcx.at(expression.span), cx.param_env)
fn check_early_return(
cx: &LateContext<'_>,
cond_expr: &Expr<'_>,
nested_expr: &Expr<'_>,
cond_ty: Ty<'_>,
ret_sym: Symbol,
) -> bool {
is_type_diagnostic_item(cx, cond_ty, ret_sym)
&& Self::expr_return_none_or_err(cx, cond_expr, nested_expr, ret_sym)
}

fn is_option(cx: &LateContext<'_>, expression: &Expr<'_>) -> bool {
let expr_ty = cx.typeck_results().expr_ty(expression);

is_type_diagnostic_item(cx, expr_ty, sym::Option)
}

fn is_result(cx: &LateContext<'_>, expression: &Expr<'_>) -> bool {
let expr_ty = cx.typeck_results().expr_ty(expression);

is_type_diagnostic_item(cx, expr_ty, sym::Result)
}

fn expression_returns_none(cx: &LateContext<'_>, expression: &Expr<'_>) -> bool {
match peel_blocks_with_stmt(expression).kind {
ExprKind::Ret(Some(expr)) => Self::expression_returns_none(cx, expr),
ExprKind::Path(ref qpath) => is_lang_ctor(cx, qpath, OptionNone),
fn expr_return_none_or_err(
cx: &LateContext<'_>,
cond_expr: &Expr<'_>,
nested_expr: &Expr<'_>,
ret_sym: Symbol,
) -> bool {
match peel_blocks_with_stmt(nested_expr).kind {
ExprKind::Ret(Some(ret_expr)) => Self::expr_return_none_or_err(cx, cond_expr, ret_expr, ret_sym),
ExprKind::Path(ref qpath) => match ret_sym {
sym::Option => is_lang_ctor(cx, qpath, OptionNone),
sym::Result => {
path_to_local(nested_expr).is_some() && path_to_local(nested_expr) == path_to_local(cond_expr)
},
_ => false,
},
_ => false,
}
}

fn expression_returns_unmodified_err(cx: &LateContext<'_>, expr: &Expr<'_>, cond_expr: &Expr<'_>) -> bool {
match peel_blocks_with_stmt(expr).kind {
ExprKind::Ret(Some(ret_expr)) => Self::expression_returns_unmodified_err(cx, ret_expr, cond_expr),
ExprKind::Path(_) => path_to_local(expr).is_some() && path_to_local(expr) == path_to_local(cond_expr),
_ => false,
fn offer_suggestion(cx: &LateContext<'_>, expr: &Expr<'_>, suggestion: String, applicability: Applicability) {
if !suggestion.is_empty() {
span_lint_and_sugg(
cx,
QUESTION_MARK,
expr.span,
"this block may be rewritten with the `?` operator",
"replace it with",
suggestion,
applicability,
);
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/question_mark.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ LL | | self.opt
LL | | };
| |_________^ help: replace it with: `Some(self.opt?)`

error: this if-let-else may be rewritten with the `?` operator
error: this block may be rewritten with the `?` operator
--> $DIR/question_mark.rs:65:17
|
LL | let _ = if let Some(x) = self.opt {
Expand Down Expand Up @@ -70,7 +70,7 @@ LL | | return None;
LL | | }
| |_________^ help: replace it with: `self.opt.as_ref()?;`

error: this if-let-else may be rewritten with the `?` operator
error: this block may be rewritten with the `?` operator
--> $DIR/question_mark.rs:105:26
|
LL | let v: &Vec<_> = if let Some(ref v) = self.opt {
Expand All @@ -81,7 +81,7 @@ LL | | return None;
LL | | };
| |_________^ help: replace it with: `self.opt.as_ref()?`

error: this if-let-else may be rewritten with the `?` operator
error: this block may be rewritten with the `?` operator
--> $DIR/question_mark.rs:115:17
|
LL | let v = if let Some(v) = self.opt {
Expand All @@ -100,7 +100,7 @@ LL | | return None;
LL | | }
| |_____^ help: replace it with: `f()?;`

error: this if-let-else may be rewritten with the `?` operator
error: this block may be rewritten with the `?` operator
--> $DIR/question_mark.rs:142:13
|
LL | let _ = if let Ok(x) = x { x } else { return x };
Expand Down

0 comments on commit 7aed5f0

Please sign in to comment.