From 1e7a67c8139b7ce6f9bf20fe6dbb917c17056fc2 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Thu, 21 Jan 2021 14:00:25 -0800 Subject: [PATCH] Fix indentation of suggestion --- clippy_lints/src/exhaustive_items.rs | 5 +++-- tests/ui/exhaustive_items.fixed | 4 ++-- tests/ui/exhaustive_items.stderr | 4 ++-- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/clippy_lints/src/exhaustive_items.rs b/clippy_lints/src/exhaustive_items.rs index e8e1e49a4108..ae01a4ceb25e 100644 --- a/clippy_lints/src/exhaustive_items.rs +++ b/clippy_lints/src/exhaustive_items.rs @@ -1,4 +1,4 @@ -use crate::utils::{snippet_opt, span_lint_and_help, span_lint_and_sugg}; +use crate::utils::{indent_of, snippet_opt, span_lint_and_help, span_lint_and_sugg}; use if_chain::if_chain; use rustc_errors::Applicability; use rustc_hir::{Item, ItemKind}; @@ -82,13 +82,14 @@ impl LateLintPass<'_> for ExhaustiveItems { }; if let Some(snippet) = snippet_opt(cx, item.span) { + let indent = " ".repeat(indent_of(cx, item.span).unwrap_or(0)); span_lint_and_sugg( cx, lint, item.span, "enums should not be exhaustive", "try adding #[non_exhaustive]", - format!("#[non_exhaustive]\n{}", snippet), + format!("#[non_exhaustive]\n{}{}", indent, snippet), Applicability::MaybeIncorrect, ); } else { diff --git a/tests/ui/exhaustive_items.fixed b/tests/ui/exhaustive_items.fixed index bc146c6afc5e..7e355d2a58b3 100644 --- a/tests/ui/exhaustive_items.fixed +++ b/tests/ui/exhaustive_items.fixed @@ -9,7 +9,7 @@ fn main() { pub mod enums { #[non_exhaustive] -pub enum Exhaustive { + pub enum Exhaustive { Foo, Bar, Baz, @@ -45,7 +45,7 @@ pub enum Exhaustive { pub mod structs { #[non_exhaustive] -pub struct Exhaustive { + pub struct Exhaustive { foo: u8, bar: String, } diff --git a/tests/ui/exhaustive_items.stderr b/tests/ui/exhaustive_items.stderr index 7e286e65949a..1716c8e2cb54 100644 --- a/tests/ui/exhaustive_items.stderr +++ b/tests/ui/exhaustive_items.stderr @@ -17,7 +17,7 @@ LL | #![deny(clippy::exhaustive_enums, clippy::exhaustive_structs)] help: try adding #[non_exhaustive] | LL | #[non_exhaustive] -LL | pub enum Exhaustive { +LL | pub enum Exhaustive { LL | Foo, LL | Bar, LL | Baz, @@ -36,7 +36,7 @@ LL | | } help: try adding #[non_exhaustive] | LL | #[non_exhaustive] -LL | pub struct Exhaustive { +LL | pub struct Exhaustive { LL | foo: u8, LL | bar: String, LL | }