]> git.lizzy.rs Git - rust.git/commitdiff
issue #8239: Printed hint for lint or_fun_call is cropped and does not show the solution
authorMarek Downar <marek.downar@evomedia.pl>
Sat, 15 Jan 2022 14:51:46 +0000 (15:51 +0100)
committerMarek Downar <marek.downar@evomedia.pl>
Sat, 15 Jan 2022 14:51:46 +0000 (15:51 +0100)
clippy_lints/src/methods/or_fun_call.rs

index 4e4653dadcafcdc1bfe926c2c9cda0a58eea697b..6fb67ff89c358330828ab20d6598045266a3b805 100644 (file)
@@ -4,6 +4,7 @@
 use clippy_utils::ty::{implements_trait, match_type};
 use clippy_utils::{contains_return, is_trait_item, last_path_segment, paths};
 use if_chain::if_chain;
+use rustc_errors::emitter::MAX_SUGGESTION_HIGHLIGHT_LINES;
 use rustc_errors::Applicability;
 use rustc_hir as hir;
 use rustc_lint::LateContext;
@@ -52,16 +53,24 @@ fn check_unwrap_or_default(
 
             then {
                 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(
                     cx,
                     OR_FUN_CALL,
                     span,
                     &format!("use of `{}` followed by a call to `{}`", name, path),
                     "try this",
-                    format!(
-                        "{}.unwrap_or_default()",
-                        snippet_with_applicability(cx, self_expr.span, "..", &mut applicability)
-                    ),
+                    sugg,
                     applicability,
                 );