about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--clippy_lints/src/exhaustive_items.rs5
-rw-r--r--tests/ui/exhaustive_items.fixed4
-rw-r--r--tests/ui/exhaustive_items.stderr4
3 files changed, 7 insertions, 6 deletions
diff --git a/clippy_lints/src/exhaustive_items.rs b/clippy_lints/src/exhaustive_items.rs
index d604ad0004f..bbcf9bfea27 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 bc146c6afc5..7e355d2a58b 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 7e286e65949..1716c8e2cb5 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 |     }