Skip to content

Commit

Permalink
Fix ICE in EarlyAttribtues lints
Browse files Browse the repository at this point in the history
  • Loading branch information
flip1995 committed May 5, 2022
1 parent bb01aca commit 0062829
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
6 changes: 5 additions & 1 deletion clippy_lints/src/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use clippy_utils::msrvs;
use clippy_utils::source::{first_line_of_span, is_present_in_source, snippet_opt, without_block_comments};
use clippy_utils::{extract_msrv_attr, meets_msrv};
use if_chain::if_chain;
use rustc_ast::{AttrKind, AttrStyle, Attribute, Lit, LitKind, MetaItemKind, NestedMetaItem};
use rustc_ast::{AttrKind, AttrStyle, Attribute, Lit, LitKind, MacArgs, MacArgsEq, MetaItemKind, NestedMetaItem};
use rustc_errors::Applicability;
use rustc_hir::{
Block, Expr, ExprKind, ImplItem, ImplItemKind, Item, ItemKind, StmtKind, TraitFn, TraitItem, TraitItemKind,
Expand Down Expand Up @@ -593,6 +593,10 @@ fn check_empty_line_after_outer_attr(cx: &EarlyContext<'_>, item: &rustc_ast::It
};

if attr.style == AttrStyle::Outer {
if let MacArgs::Eq(_, MacArgsEq::Ast(expr)) = &attr_item.args
&& !matches!(expr.kind, rustc_ast::ExprKind::Lit(..)) {
return;
}

This comment has been minimized.

Copy link
@nnethercote

nnethercote May 5, 2022

Contributor

Thanks for fixing this issue! This change is enough to avoid the crash, but I think this would be better:

        if attr.style == AttrStyle::Outer {
            if matches!(attr_item.args, MacArgs::Empty) || !is_present_in_source(cx, attr.span) {
                return;
            }

            let begin_of_attr_to_item = Span::new(attr.span.lo(), item.span.lo(), item.span.ctxt(), 
item.span.parent());

It's simpler and avoids an unnecessary early return.

This comment has been minimized.

Copy link
@flip1995

flip1995 May 6, 2022

Author Member

We already removed this check completely, as it was legacy code that wasn't necessary anymore, see #8790

Thanks for the hint nonetheless!

if attr_item.args.inner_tokens().is_empty() || !is_present_in_source(cx, attr.span) {
return;
}
Expand Down
10 changes: 10 additions & 0 deletions tests/ui/crashes/ice-96721.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
macro_rules! foo {
() => {
"bar.rs"
};
}

#[path = foo!()] //~ ERROR malformed `path` attribute
mod abc {}

fn main() {}
8 changes: 8 additions & 0 deletions tests/ui/crashes/ice-96721.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: malformed `path` attribute input
--> $DIR/ice-96721.rs:7:1
|
LL | #[path = foo!()] //~ ERROR malformed `path` attribute
| ^^^^^^^^^^^^^^^^ help: must be of the form: `#[path = "file"]`

error: aborting due to previous error

0 comments on commit 0062829

Please sign in to comment.