Skip to content

Commit 60c1bb0

Browse files
committed
Fix ICE in suspicious_else_formatting
1 parent b996fd5 commit 60c1bb0

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

clippy_lints/src/formatting.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::utils::{differing_macro_contexts, in_macro, snippet_opt, span_note_and_lint};
2-
use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
2+
use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass, in_external_macro};
33
use rustc::{declare_tool_lint, lint_array};
44
use syntax::ast;
55
use syntax::ptr::P;
@@ -149,7 +149,7 @@ fn check_else(cx: &EarlyContext<'_>, expr: &ast::Expr) {
149149
if let Some((then, &Some(ref else_))) = unsugar_if(expr) {
150150
if (is_block(else_) || unsugar_if(else_).is_some())
151151
&& !differing_macro_contexts(then.span, else_.span)
152-
&& !in_macro(then.span)
152+
&& !in_macro(then.span) && !in_external_macro(cx.sess, expr.span)
153153
{
154154
// workaround for rust-lang/rust#43081
155155
if expr.span.lo().0 == 0 && expr.span.hi().0 == 0 {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// force-host
2+
// no-prefer-dynamic
3+
4+
#![feature(repr128)]
5+
#![crate_type = "proc-macro"]
6+
7+
extern crate proc_macro;
8+
9+
use proc_macro::{Delimiter, Group, Ident, Span, TokenStream, TokenTree};
10+
use std::iter::FromIterator;
11+
12+
#[proc_macro]
13+
pub fn macro_test(input_stream: TokenStream) -> TokenStream {
14+
let first_token = input_stream.into_iter().next().unwrap();
15+
let span = first_token.span();
16+
17+
TokenStream::from_iter(vec![
18+
TokenTree::Ident(Ident::new("fn", Span::call_site())),
19+
TokenTree::Ident(Ident::new("code", Span::call_site())),
20+
TokenTree::Group(Group::new(Delimiter::Parenthesis, TokenStream::new())),
21+
TokenTree::Group(Group::new(Delimiter::Brace, {
22+
let mut clause = Group::new(Delimiter::Brace, TokenStream::new());
23+
clause.set_span(span);
24+
25+
TokenStream::from_iter(vec![
26+
TokenTree::Ident(Ident::new("if", Span::call_site())),
27+
TokenTree::Ident(Ident::new("true", Span::call_site())),
28+
TokenTree::Group(clause.clone()),
29+
TokenTree::Ident(Ident::new("else", Span::call_site())),
30+
TokenTree::Group(clause.clone()),
31+
])
32+
})),
33+
])
34+
}

tests/ui/crashes/ice-3741.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// aux-build:proc_macro_crash.rs
2+
// run-pass
3+
4+
#![feature(proc_macro_hygiene)]
5+
#![warn(clippy::suspicious_else_formatting)]
6+
7+
extern crate proc_macro_crash;
8+
use proc_macro_crash::macro_test;
9+
10+
fn main() {
11+
macro_test!(2);
12+
}

0 commit comments

Comments
 (0)