Skip to content

Commit

Permalink
remove duplicate diags for deprecated attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
grtn316 committed Jun 19, 2024
1 parent 9f5d60f commit 56b1a0f
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 27 deletions.
2 changes: 2 additions & 0 deletions clippy_utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ clippy_config = { path = "../clippy_config" }
arrayvec = { version = "0.7", default-features = false }
itertools = "0.12"
rustc-semver = "1.1"
rustc-hash = "1.1"
lazy_static = "1.4"

[features]
deny-warnings = ["clippy_config/deny-warnings"]
Expand Down
110 changes: 86 additions & 24 deletions clippy_utils/src/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,48 @@ use rustc_middle::ty::{AdtDef, TyCtxt};
use rustc_session::Session;
use rustc_span::{sym, Span};
use std::str::FromStr;
use lazy_static::lazy_static;
use rustc_hash::FxHashSet;
use std::sync::RwLock;

use std::io::{self, Write};


use crate::source::snippet_opt;
use crate::tokenize_with_text;


pub struct EmissionState {
emitted: RwLock<FxHashSet<String>>,
}

impl EmissionState {
pub fn new() -> Self {
Self {
emitted: RwLock::new(FxHashSet::default()),
}
}

pub fn has_emitted(&self, attr_name: &str) -> bool {
let emitted = self.emitted.read().unwrap();
emitted.contains(attr_name)
}

pub fn set_emitted(&self, attr_name: &str) {
let mut emitted = self.emitted.write().unwrap();
emitted.insert(attr_name.to_string());
}

pub fn reset(&self) {
let mut emitted = self.emitted.write().unwrap();
emitted.clear();
}
}

lazy_static! {
pub static ref GLOBAL_EMISSION_STATE: EmissionState = EmissionState::new();
}

/// Deprecation status of attributes known by Clippy.
pub enum DeprecationStatus {
/// Attribute is deprecated
Expand Down Expand Up @@ -58,6 +96,8 @@ impl LimitStack {
}
}



pub fn get_attr<'a>(
sess: &'a Session,
attrs: &'a [ast::Attribute],
Expand All @@ -69,8 +109,15 @@ pub fn get_attr<'a>(
} else {
return false;
};


let attr_segments = &attr.path.segments;
if attr_segments.len() == 2 && attr_segments[0].ident.name == sym::clippy {


if attr_segments.len() == 2 && attr_segments[0].ident.name == sym::clippy {
let attr_name = attr_segments[1].ident.name.as_str().to_string();


BUILTIN_ATTRIBUTES
.iter()
.find_map(|&(builtin_name, ref deprecation_status)| {
Expand All @@ -82,36 +129,51 @@ pub fn get_attr<'a>(
})
.map_or_else(
|| {

sess.dcx()
.span_err(attr_segments[1].ident.span, "usage of unknown attribute");
false
},
|deprecation_status| {
let mut diag = sess
.dcx()
.struct_span_err(attr_segments[1].ident.span, "usage of deprecated attribute");
match *deprecation_status {
DeprecationStatus::Deprecated => {
diag.emit();
false
},
DeprecationStatus::Replaced(new_name) => {
diag.span_suggestion(
attr_segments[1].ident.span,
"consider using",
new_name,
Applicability::MachineApplicable,
);
diag.emit();
false
},
DeprecationStatus::None => {
diag.cancel();
attr_segments[1].ident.name.as_str() == name
},

if !GLOBAL_EMISSION_STATE.has_emitted(&attr_name) {
let mut diag = sess
.dcx()
.struct_span_err(attr_segments[1].ident.span, "usage of deprecated attribute");

match *deprecation_status {
DeprecationStatus::Deprecated => {
GLOBAL_EMISSION_STATE.set_emitted(&attr_name);
diag.emit();

io::stderr().flush().unwrap(); // Flush stderr
false
},
DeprecationStatus::Replaced(new_name) => {
GLOBAL_EMISSION_STATE.set_emitted(&attr_name);
diag.span_suggestion(
attr_segments[1].ident.span,
"consider using",
new_name,
Applicability::MachineApplicable,
);
diag.emit();

io::stderr().flush().unwrap(); // Flush stderr
false
},
DeprecationStatus::None => {
diag.cancel();
attr_segments[1].ident.name.as_str() == name
},
}
} else {
false
}
},
)

)

} else {
false
}
Expand Down
1 change: 0 additions & 1 deletion tests/ui/renamed_builtin_attr.fixed
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//@compile-flags: -Zdeduplicate-diagnostics=yes

#[clippy::cognitive_complexity = "1"]
fn main() {}
1 change: 0 additions & 1 deletion tests/ui/renamed_builtin_attr.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//@compile-flags: -Zdeduplicate-diagnostics=yes

#[clippy::cyclomatic_complexity = "1"]
fn main() {}
2 changes: 1 addition & 1 deletion tests/ui/renamed_builtin_attr.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: usage of deprecated attribute
--> tests/ui/renamed_builtin_attr.rs:3:11
--> tests/ui/renamed_builtin_attr.rs:2:11
|
LL | #[clippy::cyclomatic_complexity = "1"]
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `cognitive_complexity`
Expand Down

0 comments on commit 56b1a0f

Please sign in to comment.