Skip to content

Commit 5464cbe

Browse files
committed
Auto merge of rust-lang#6337 - ThibsG:FixIce6332, r=Manishearth
Remove `expect()` calls to avoid ICEs in `deref_addrof` lint Fixes: rust-lang#6332 changelog: none
2 parents a8cafc6 + 5b8f2b6 commit 5464cbe

File tree

2 files changed

+39
-20
lines changed

2 files changed

+39
-20
lines changed

clippy_lints/src/reference.rs

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -60,30 +60,38 @@ impl EarlyLintPass for DerefAddrOf {
6060
}).map_or(span, |start_no_whitespace| e.span.with_lo(start_no_whitespace))
6161
};
6262

63-
let rpos = if *mutability == Mutability::Mut {
64-
macro_source.rfind("mut").expect("already checked this is a mutable reference") + "mut".len()
65-
} else {
66-
macro_source.rfind('&').expect("already checked this is a reference") + "&".len()
63+
let mut generate_snippet = |pattern: &str| {
64+
#[allow(clippy::cast_possible_truncation)]
65+
macro_source.rfind(pattern).map(|pattern_pos| {
66+
let rpos = pattern_pos + pattern.len();
67+
let span_after_ref = e.span.with_lo(BytePos(e.span.lo().0 + rpos as u32));
68+
let span = trim_leading_whitespaces(span_after_ref);
69+
snippet_with_applicability(cx, span, "_", &mut applicability)
70+
})
6771
};
68-
#[allow(clippy::cast_possible_truncation)]
69-
let span_after_ref = e.span.with_lo(BytePos(e.span.lo().0 + rpos as u32));
70-
let span = trim_leading_whitespaces(span_after_ref);
71-
snippet_with_applicability(cx, span, "_", &mut applicability)
72+
73+
if *mutability == Mutability::Mut {
74+
generate_snippet("mut")
75+
} else {
76+
generate_snippet("&")
77+
}
7278
} else {
73-
snippet_with_applicability(cx, e.span, "_", &mut applicability)
79+
Some(snippet_with_applicability(cx, e.span, "_", &mut applicability))
7480
}
7581
} else {
76-
snippet_with_applicability(cx, addrof_target.span, "_", &mut applicability)
77-
}.to_string();
78-
span_lint_and_sugg(
79-
cx,
80-
DEREF_ADDROF,
81-
e.span,
82-
"immediately dereferencing a reference",
83-
"try this",
84-
sugg,
85-
applicability,
86-
);
82+
Some(snippet_with_applicability(cx, addrof_target.span, "_", &mut applicability))
83+
};
84+
if let Some(sugg) = sugg {
85+
span_lint_and_sugg(
86+
cx,
87+
DEREF_ADDROF,
88+
e.span,
89+
"immediately dereferencing a reference",
90+
"try this",
91+
sugg.to_string(),
92+
applicability,
93+
);
94+
}
8795
}
8896
}
8997
}

tests/ui/crashes/ice-6332.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
fn cmark_check() {
2+
let mut link_err = false;
3+
macro_rules! cmark_error {
4+
($bad:expr) => {
5+
*$bad = true;
6+
};
7+
}
8+
cmark_error!(&mut link_err);
9+
}
10+
11+
pub fn main() {}

0 commit comments

Comments
 (0)