Skip to content

Commit

Permalink
add manual_ok_or / pr remarks
Browse files Browse the repository at this point in the history
  • Loading branch information
tnielens committed Oct 29, 2020
1 parent 111b902 commit d780f61
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions clippy_lints/src/manual_ok_or.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::utils;
use crate::utils::{
indent_of, is_type_diagnostic_item, match_qpath, paths, reindent_multiline, snippet_opt, span_lint_and_sugg,
};
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir::{def, Expr, ExprKind, PatKind, QPath};
Expand Down Expand Up @@ -49,18 +51,18 @@ impl LateLintPass<'_> for ManualOkOr {
if args.len() == 3;
let method_receiver = &args[0];
let ty = cx.typeck_results().expr_ty(method_receiver);
if utils::is_type_diagnostic_item(cx, ty, sym!(option_type));
if is_type_diagnostic_item(cx, ty, sym!(option_type));
let or_expr = &args[1];
if is_ok_wrapping(cx, &args[2]);
if let ExprKind::Call(Expr { kind: ExprKind::Path(err_path), .. }, &[ref err_arg]) = or_expr.kind;
if utils::match_qpath(err_path, &utils::paths::RESULT_ERR);
if let Some(method_receiver_snippet) = utils::snippet_opt(cx, method_receiver.span);
if let Some(err_arg_snippet) = utils::snippet_opt(cx, err_arg.span);
if let Some(indent) = utils::indent_of(cx, scrutinee.span);
if match_qpath(err_path, &paths::RESULT_ERR);
if let Some(method_receiver_snippet) = snippet_opt(cx, method_receiver.span);
if let Some(err_arg_snippet) = snippet_opt(cx, err_arg.span);
if let Some(indent) = indent_of(cx, scrutinee.span);
then {
let reindented_err_arg_snippet =
utils::reindent_multiline(err_arg_snippet.into(), true, Some(indent + 4));
utils::span_lint_and_sugg(
reindent_multiline(err_arg_snippet.into(), true, Some(indent + 4));
span_lint_and_sugg(
cx,
MANUAL_OK_OR,
scrutinee.span,
Expand All @@ -80,7 +82,7 @@ impl LateLintPass<'_> for ManualOkOr {

fn is_ok_wrapping(cx: &LateContext<'_>, map_expr: &Expr<'_>) -> bool {
if let ExprKind::Path(ref qpath) = map_expr.kind {
if utils::match_qpath(qpath, &utils::paths::RESULT_OK) {
if match_qpath(qpath, &paths::RESULT_OK) {
return true;
}
}
Expand All @@ -89,7 +91,7 @@ fn is_ok_wrapping(cx: &LateContext<'_>, map_expr: &Expr<'_>) -> bool {
let body = cx.tcx.hir().body(body_id);
if let PatKind::Binding(_, param_id, ..) = body.params[0].pat.kind;
if let ExprKind::Call(Expr { kind: ExprKind::Path(ok_path), .. }, &[ref ok_arg]) = body.value.kind;
if utils::match_qpath(ok_path, &utils::paths::RESULT_OK);
if match_qpath(ok_path, &paths::RESULT_OK);
if let ExprKind::Path(QPath::Resolved(_, ok_arg_path)) = ok_arg.kind;
if let def::Res::Local(ok_arg_path_id) = ok_arg_path.res;
then { param_id == ok_arg_path_id } else { false }
Expand Down

0 comments on commit d780f61

Please sign in to comment.