issue #8239: Printed hint for lint or_fun_call is cropped and does not show the solution

This commit is contained in:
Marek Downar 2022-01-15 15:51:46 +01:00
parent 5cada57f30
commit ee84ac3396
No known key found for this signature in database
GPG key ID: 62D7E3EF71CA85B2

View file

@ -4,6 +4,7 @@ use clippy_utils::source::{snippet, snippet_with_applicability, snippet_with_mac
use clippy_utils::ty::{implements_trait, match_type}; use clippy_utils::ty::{implements_trait, match_type};
use clippy_utils::{contains_return, is_trait_item, last_path_segment, paths}; use clippy_utils::{contains_return, is_trait_item, last_path_segment, paths};
use if_chain::if_chain; use if_chain::if_chain;
use rustc_errors::emitter::MAX_SUGGESTION_HIGHLIGHT_LINES;
use rustc_errors::Applicability; use rustc_errors::Applicability;
use rustc_hir as hir; use rustc_hir as hir;
use rustc_lint::LateContext; use rustc_lint::LateContext;
@ -52,16 +53,24 @@ pub(super) fn check<'tcx>(
then { then {
let mut applicability = Applicability::MachineApplicable; let mut applicability = Applicability::MachineApplicable;
let hint = ".unwrap_or_default()";
let mut sugg: String = format!(
"{}{}",
snippet_with_applicability(cx, self_expr.span, "..", &mut applicability),
hint
);
if sugg.lines().count() > MAX_SUGGESTION_HIGHLIGHT_LINES {
sugg = hint.to_string();
}
span_lint_and_sugg( span_lint_and_sugg(
cx, cx,
OR_FUN_CALL, OR_FUN_CALL,
span, span,
&format!("use of `{}` followed by a call to `{}`", name, path), &format!("use of `{}` followed by a call to `{}`", name, path),
"try this", "try this",
format!( sugg,
"{}.unwrap_or_default()",
snippet_with_applicability(cx, self_expr.span, "..", &mut applicability)
),
applicability, applicability,
); );