mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-17 02:08:28 +00:00
Clean up suggestion span; clarify help message
This commit is contained in:
parent
752274eabd
commit
65d003a112
2 changed files with 26 additions and 23 deletions
|
@ -1,4 +1,4 @@
|
|||
use crate::utils::{indent_of, snippet_opt, span_lint_and_help, span_lint_and_sugg};
|
||||
use crate::utils::{indent_of, snippet_opt, span_lint_and_help, span_lint_and_then};
|
||||
use if_chain::if_chain;
|
||||
use rustc_errors::Applicability;
|
||||
use rustc_hir::{Item, ItemKind};
|
||||
|
@ -75,29 +75,34 @@ impl LateLintPass<'_> for ExhaustiveItems {
|
|||
if cx.access_levels.is_exported(item.hir_id);
|
||||
if !item.attrs.iter().any(|a| a.has_name(sym::non_exhaustive));
|
||||
then {
|
||||
let lint = if let ItemKind::Enum(..) = item.kind {
|
||||
EXHAUSTIVE_ENUMS
|
||||
let (lint, msg) = if let ItemKind::Enum(..) = item.kind {
|
||||
(EXHAUSTIVE_ENUMS, "exported enums should not be exhaustive")
|
||||
} else {
|
||||
EXHAUSTIVE_STRUCTS
|
||||
(EXHAUSTIVE_STRUCTS, "exported structs should not be exhaustive")
|
||||
};
|
||||
let suggestion_span = item.span.until(item.ident.span);
|
||||
|
||||
if let Some(snippet) = snippet_opt(cx, item.span) {
|
||||
if let Some(snippet) = snippet_opt(cx, suggestion_span) {
|
||||
let indent = " ".repeat(indent_of(cx, item.span).unwrap_or(0));
|
||||
span_lint_and_sugg(
|
||||
span_lint_and_then(
|
||||
cx,
|
||||
lint,
|
||||
item.span,
|
||||
"enums should not be exhaustive",
|
||||
"try adding #[non_exhaustive]",
|
||||
format!("#[non_exhaustive]\n{}{}", indent, snippet),
|
||||
Applicability::MaybeIncorrect,
|
||||
msg,
|
||||
|diag| {
|
||||
let sugg = format!("#[non_exhaustive]\n{}{}", indent, snippet);
|
||||
diag.span_suggestion(suggestion_span,
|
||||
"try adding #[non_exhaustive]",
|
||||
sugg,
|
||||
Applicability::MaybeIncorrect);
|
||||
}
|
||||
);
|
||||
} else {
|
||||
span_lint_and_help(
|
||||
cx,
|
||||
lint,
|
||||
item.span,
|
||||
"enums should not be exhaustive",
|
||||
msg,
|
||||
None,
|
||||
"try adding #[non_exhaustive]",
|
||||
);
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
error: enums should not be exhaustive
|
||||
error: exported enums should not be exhaustive
|
||||
--> $DIR/exhaustive_items.rs:11:5
|
||||
|
|
||||
LL | / pub enum Exhaustive {
|
||||
|
@ -10,21 +10,17 @@ LL | | }
|
|||
| |_____^
|
||||
|
|
||||
note: the lint level is defined here
|
||||
--> $DIR/exhaustive_items.rs:3:35
|
||||
--> $DIR/exhaustive_items.rs:3:9
|
||||
|
|
||||
LL | #![deny(clippy::exhaustive_enums, clippy::exhaustive_structs)]
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
help: try adding #[non_exhaustive]
|
||||
|
|
||||
LL | #[non_exhaustive]
|
||||
LL | pub enum Exhaustive {
|
||||
LL | Foo,
|
||||
LL | Bar,
|
||||
LL | Baz,
|
||||
LL | Quux(String),
|
||||
...
|
||||
|
|
||||
|
||||
error: enums should not be exhaustive
|
||||
error: exported structs should not be exhaustive
|
||||
--> $DIR/exhaustive_items.rs:46:5
|
||||
|
|
||||
LL | / pub struct Exhaustive {
|
||||
|
@ -33,13 +29,15 @@ LL | | bar: String,
|
|||
LL | | }
|
||||
| |_____^
|
||||
|
|
||||
note: the lint level is defined here
|
||||
--> $DIR/exhaustive_items.rs:3:35
|
||||
|
|
||||
LL | #![deny(clippy::exhaustive_enums, clippy::exhaustive_structs)]
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
help: try adding #[non_exhaustive]
|
||||
|
|
||||
LL | #[non_exhaustive]
|
||||
LL | pub struct Exhaustive {
|
||||
LL | foo: u8,
|
||||
LL | bar: String,
|
||||
LL | }
|
||||
|
|
||||
|
||||
error: aborting due to 2 previous errors
|
||||
|
|
Loading…
Reference in a new issue