Skip to content

Commit c230ca1

Browse files
committed
Report the unsafe_attr_outside_unsafe lint at the closest node
1 parent 74a17fd commit c230ca1

File tree

5 files changed

+36
-8
lines changed

5 files changed

+36
-8
lines changed

compiler/rustc_ast_passes/src/ast_validation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,7 @@ fn validate_generic_param_order(dcx: DiagCtxtHandle<'_>, generics: &[GenericPara
828828

829829
impl<'a> Visitor<'a> for AstValidator<'a> {
830830
fn visit_attribute(&mut self, attr: &Attribute) {
831-
validate_attr::check_attr(&self.sess.psess, attr);
831+
validate_attr::check_attr(&self.sess.psess, attr, self.lint_node_id);
832832
}
833833

834834
fn visit_ty(&mut self, ty: &'a Ty) {

compiler/rustc_expand/src/config.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,12 @@ impl<'a> StripUnconfigured<'a> {
274274
/// is in the original source file. Gives a compiler error if the syntax of
275275
/// the attribute is incorrect.
276276
pub(crate) fn expand_cfg_attr(&self, cfg_attr: &Attribute, recursive: bool) -> Vec<Attribute> {
277-
validate_attr::check_attribute_safety(&self.sess.psess, AttributeSafety::Normal, &cfg_attr);
277+
validate_attr::check_attribute_safety(
278+
&self.sess.psess,
279+
AttributeSafety::Normal,
280+
&cfg_attr,
281+
ast::CRATE_NODE_ID,
282+
);
278283

279284
// A trace attribute left in AST in place of the original `cfg_attr` attribute.
280285
// It can later be used by lints or other diagnostics.

compiler/rustc_expand/src/expand.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1983,7 +1983,11 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
19831983
let mut span: Option<Span> = None;
19841984
while let Some(attr) = attrs.next() {
19851985
rustc_ast_passes::feature_gate::check_attribute(attr, self.cx.sess, features);
1986-
validate_attr::check_attr(&self.cx.sess.psess, attr);
1986+
validate_attr::check_attr(
1987+
&self.cx.sess.psess,
1988+
attr,
1989+
self.cx.current_expansion.lint_node_id,
1990+
);
19871991

19881992
let current_span = if let Some(sp) = span { sp.to(attr.span) } else { attr.span };
19891993
span = Some(current_span);

compiler/rustc_parse/src/validate_attr.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
use rustc_ast::token::Delimiter;
44
use rustc_ast::tokenstream::DelimSpan;
55
use rustc_ast::{
6-
self as ast, AttrArgs, Attribute, DelimArgs, MetaItem, MetaItemInner, MetaItemKind, Safety,
6+
self as ast, AttrArgs, Attribute, DelimArgs, MetaItem, MetaItemInner, MetaItemKind, NodeId,
7+
Safety,
78
};
89
use rustc_errors::{Applicability, FatalError, PResult};
910
use rustc_feature::{AttributeSafety, AttributeTemplate, BUILTIN_ATTRIBUTE_MAP, BuiltinAttribute};
@@ -15,7 +16,7 @@ use rustc_span::{Span, Symbol, sym};
1516

1617
use crate::{errors, parse_in};
1718

18-
pub fn check_attr(psess: &ParseSess, attr: &Attribute) {
19+
pub fn check_attr(psess: &ParseSess, attr: &Attribute, id: NodeId) {
1920
if attr.is_doc_comment() || attr.has_name(sym::cfg_trace) || attr.has_name(sym::cfg_attr_trace)
2021
{
2122
return;
@@ -26,7 +27,7 @@ pub fn check_attr(psess: &ParseSess, attr: &Attribute) {
2627

2728
// All non-builtin attributes are considered safe
2829
let safety = attr_info.map(|x| x.safety).unwrap_or(AttributeSafety::Normal);
29-
check_attribute_safety(psess, safety, attr);
30+
check_attribute_safety(psess, safety, attr, id);
3031

3132
// Check input tokens for built-in and key-value attributes.
3233
match attr_info {
@@ -154,7 +155,12 @@ fn is_attr_template_compatible(template: &AttributeTemplate, meta: &ast::MetaIte
154155
}
155156
}
156157

157-
pub fn check_attribute_safety(psess: &ParseSess, safety: AttributeSafety, attr: &Attribute) {
158+
pub fn check_attribute_safety(
159+
psess: &ParseSess,
160+
safety: AttributeSafety,
161+
attr: &Attribute,
162+
id: NodeId,
163+
) {
158164
let attr_item = attr.get_normal_item();
159165

160166
if let AttributeSafety::Unsafe { unsafe_since } = safety {
@@ -185,7 +191,7 @@ pub fn check_attribute_safety(psess: &ParseSess, safety: AttributeSafety, attr:
185191
psess.buffer_lint(
186192
UNSAFE_ATTR_OUTSIDE_UNSAFE,
187193
path_span,
188-
ast::CRATE_NODE_ID,
194+
id,
189195
BuiltinLintDiag::UnsafeAttrOutsideUnsafe {
190196
attribute_name_span: path_span,
191197
sugg_spans: (diag_span.shrink_to_lo(), diag_span.shrink_to_hi()),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//@ check-pass
2+
//
3+
// https://github.com/rust-lang/rust/issues/140602
4+
5+
#![deny(unsafe_attr_outside_unsafe)]
6+
7+
#[allow(unsafe_attr_outside_unsafe)]
8+
mod generated {
9+
#[no_mangle]
10+
fn _generated_foo() {}
11+
}
12+
13+
fn main() {}

0 commit comments

Comments
 (0)