Skip to content

Commit

Permalink
fix test fail caused by api change
Browse files Browse the repository at this point in the history
And some minor changes.
  • Loading branch information
J-ZhengLi committed Mar 15, 2022
1 parent 148a04d commit 1330ad9
Showing 1 changed file with 28 additions and 34 deletions.
62 changes: 28 additions & 34 deletions clippy_lints/src/methods/map_flatten.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,20 @@ pub(super) fn check<'tcx>(
map_span: Span,
) {
if let Some((caller_ty_name, method_to_use)) = try_get_caller_ty_name_and_method_name(cx, expr, recv, map_arg) {
let mut applicability = Applicability::MachineApplicable;
let sugg_span = expr.span.with_lo(map_span.lo());
let msg = format!("called `map(..).flatten()` on an `{}`", caller_ty_name);
let help_msgs = [
&format!("try replacing `map` with `{}`", method_to_use),
"and remove the `.flatten()`",
];
let mut applicability = Applicability::MachineApplicable;
let closure_snippet = snippet_with_applicability(cx, map_arg.span, "..", &mut applicability);
let sugg = format!("{}({})", method_to_use, closure_snippet);
span_lint_and_splitted_sugg(cx, sugg_span, &msg, &help_msgs, sugg, applicability);
span_lint_and_splitted_sugg(
cx,
expr.span.with_lo(map_span.lo()),
&format!("called `map(..).flatten()` on an `{}`", caller_ty_name),
&help_msgs,
format!("{}({})", method_to_use, closure_snippet),
applicability,
);
}
}

Expand All @@ -50,9 +54,9 @@ fn try_get_caller_ty_name_and_method_name(
}
} else {
if let ty::Adt(adt, _) = cx.typeck_results().expr_ty(caller_expr).kind() {
if cx.tcx.is_diagnostic_item(sym::Option, adt.did) {
if cx.tcx.is_diagnostic_item(sym::Option, adt.did()) {
return Some(("Option".to_string(), "and_then".to_string()));
} else if cx.tcx.is_diagnostic_item(sym::Result, adt.did) {
} else if cx.tcx.is_diagnostic_item(sym::Result, adt.did()) {
return Some(("Result".to_string(), "and_then".to_string()));
}
}
Expand All @@ -77,7 +81,7 @@ fn is_map_to_option(cx: &LateContext<'_>, map_arg: &Expr<'_>) -> bool {

/// Split code suggestion into upper and bottom parts if its number of lines exceeds
/// `MAX_SUGGESTION_HIGHLIGHT_LINES`, otherwise this function will just call `span_lint_and_sugg`
/// with the combined elements provided in `help`.
/// with a combined `help` message.
///
/// This is useful when a suggested code snippet has multiple line thus causing possible useful
/// information being chopped off at the end.
Expand All @@ -90,36 +94,26 @@ fn span_lint_and_splitted_sugg(
applicability: Applicability,
) {
let sugg_lines_count = sugg.lines().count();
let mut no_split = false;
if sugg_lines_count > MAX_SUGGESTION_HIGHLIGHT_LINES {
let sm = cx.sess().source_map();
match (sm.lookup_line(sp.lo()), sm.lookup_line(sp.hi())) {
(Ok(src_line_upper), Ok(src_line_bottom)) => {
let split_idx = MAX_SUGGESTION_HIGHLIGHT_LINES / 2;
let span_upper = sp.with_hi(src_line_upper.sf.lines[src_line_upper.line + split_idx]);
// covering the reset of the last line of `span_upper` as well
let span_upper = sm.span_until_char(span_upper, '\n');
let span_bottom = sp.with_lo(src_line_bottom.sf.lines[src_line_bottom.line - split_idx]);
if let (Ok(src_line_upper), Ok(src_line_bottom)) = (sm.lookup_line(sp.lo()), sm.lookup_line(sp.hi())) {
let split_idx = MAX_SUGGESTION_HIGHLIGHT_LINES / 2;
let span_upper = sp.with_hi(src_line_upper.sf.lines[src_line_upper.line + split_idx]);
// span over the rest of the last line of `span_upper`
let span_upper = sm.span_until_char(span_upper, '\n');
let span_bottom = sp.with_lo(src_line_bottom.sf.lines[src_line_bottom.line - split_idx]);

let sugg_lines_vec = sugg.lines().collect::<Vec<&str>>();
let sugg_upper = sugg_lines_vec[..split_idx].join("\n");
let sugg_bottom = sugg_lines_vec[sugg_lines_count - split_idx..].join("\n");
let sugg_lines_vec = sugg.lines().collect::<Vec<&str>>();
let sugg_upper = sugg_lines_vec[..split_idx].join("\n");
let sugg_bottom = sugg_lines_vec[sugg_lines_count - split_idx..].join("\n");

span_lint_and_then(cx, MAP_FLATTEN, sp, msg, |diag| {
diag.span_suggestion(span_upper, help[0], sugg_upper, applicability);
diag.span_suggestion(span_bottom, help[1], sugg_bottom, applicability);
});
},
_ => {
no_split = true;
},
span_lint_and_then(cx, MAP_FLATTEN, sp, msg, |diag| {
diag.span_suggestion(span_upper, help[0], sugg_upper, applicability);
diag.span_suggestion(span_bottom, help[1], sugg_bottom, applicability);
});
return;
}
} else {
no_split = true;
}

if no_split {
let combined_help = help.join(", ");
span_lint_and_sugg(cx, MAP_FLATTEN, sp, msg, &combined_help, sugg, applicability);
}
let combined_help = help.join(", ");
span_lint_and_sugg(cx, MAP_FLATTEN, sp, msg, &combined_help, sugg, applicability);
}

0 comments on commit 1330ad9

Please sign in to comment.