Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 65d003a

Browse files
committed
Clean up suggestion span; clarify help message
1 parent 752274e commit 65d003a

File tree

2 files changed

+26
-23
lines changed

2 files changed

+26
-23
lines changed

clippy_lints/src/exhaustive_items.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::utils::{indent_of, snippet_opt, span_lint_and_help, span_lint_and_sugg};
1+
use crate::utils::{indent_of, snippet_opt, span_lint_and_help, span_lint_and_then};
22
use if_chain::if_chain;
33
use rustc_errors::Applicability;
44
use rustc_hir::{Item, ItemKind};
@@ -75,29 +75,34 @@ impl LateLintPass<'_> for ExhaustiveItems {
7575
if cx.access_levels.is_exported(item.hir_id);
7676
if !item.attrs.iter().any(|a| a.has_name(sym::non_exhaustive));
7777
then {
78-
let lint = if let ItemKind::Enum(..) = item.kind {
79-
EXHAUSTIVE_ENUMS
78+
let (lint, msg) = if let ItemKind::Enum(..) = item.kind {
79+
(EXHAUSTIVE_ENUMS, "exported enums should not be exhaustive")
8080
} else {
81-
EXHAUSTIVE_STRUCTS
81+
(EXHAUSTIVE_STRUCTS, "exported structs should not be exhaustive")
8282
};
83+
let suggestion_span = item.span.until(item.ident.span);
8384

84-
if let Some(snippet) = snippet_opt(cx, item.span) {
85+
if let Some(snippet) = snippet_opt(cx, suggestion_span) {
8586
let indent = " ".repeat(indent_of(cx, item.span).unwrap_or(0));
86-
span_lint_and_sugg(
87+
span_lint_and_then(
8788
cx,
8889
lint,
8990
item.span,
90-
"enums should not be exhaustive",
91-
"try adding #[non_exhaustive]",
92-
format!("#[non_exhaustive]\n{}{}", indent, snippet),
93-
Applicability::MaybeIncorrect,
91+
msg,
92+
|diag| {
93+
let sugg = format!("#[non_exhaustive]\n{}{}", indent, snippet);
94+
diag.span_suggestion(suggestion_span,
95+
"try adding #[non_exhaustive]",
96+
sugg,
97+
Applicability::MaybeIncorrect);
98+
}
9499
);
95100
} else {
96101
span_lint_and_help(
97102
cx,
98103
lint,
99104
item.span,
100-
"enums should not be exhaustive",
105+
msg,
101106
None,
102107
"try adding #[non_exhaustive]",
103108
);

tests/ui/exhaustive_items.stderr

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: enums should not be exhaustive
1+
error: exported enums should not be exhaustive
22
--> $DIR/exhaustive_items.rs:11:5
33
|
44
LL | / pub enum Exhaustive {
@@ -10,21 +10,17 @@ LL | | }
1010
| |_____^
1111
|
1212
note: the lint level is defined here
13-
--> $DIR/exhaustive_items.rs:3:35
13+
--> $DIR/exhaustive_items.rs:3:9
1414
|
1515
LL | #![deny(clippy::exhaustive_enums, clippy::exhaustive_structs)]
16-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
16+
| ^^^^^^^^^^^^^^^^^^^^^^^^
1717
help: try adding #[non_exhaustive]
1818
|
1919
LL | #[non_exhaustive]
2020
LL | pub enum Exhaustive {
21-
LL | Foo,
22-
LL | Bar,
23-
LL | Baz,
24-
LL | Quux(String),
25-
...
21+
|
2622

27-
error: enums should not be exhaustive
23+
error: exported structs should not be exhaustive
2824
--> $DIR/exhaustive_items.rs:46:5
2925
|
3026
LL | / pub struct Exhaustive {
@@ -33,13 +29,15 @@ LL | | bar: String,
3329
LL | | }
3430
| |_____^
3531
|
32+
note: the lint level is defined here
33+
--> $DIR/exhaustive_items.rs:3:35
34+
|
35+
LL | #![deny(clippy::exhaustive_enums, clippy::exhaustive_structs)]
36+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
3637
help: try adding #[non_exhaustive]
3738
|
3839
LL | #[non_exhaustive]
3940
LL | pub struct Exhaustive {
40-
LL | foo: u8,
41-
LL | bar: String,
42-
LL | }
4341
|
4442

4543
error: aborting due to 2 previous errors

0 commit comments

Comments
 (0)