Auto merge of #6337 - ThibsG:FixIce6332, r=Manishearth

Remove `expect()` calls to avoid ICEs in `deref_addrof` lint

Fixes: #6332

changelog: none
This commit is contained in:
bors
2020-11-17 17:55:46 +00:00
2 changed files with 39 additions and 20 deletions

View File

@@ -60,30 +60,38 @@ impl EarlyLintPass for DerefAddrOf {
}).map_or(span, |start_no_whitespace| e.span.with_lo(start_no_whitespace)) }).map_or(span, |start_no_whitespace| e.span.with_lo(start_no_whitespace))
}; };
let rpos = if *mutability == Mutability::Mut { let mut generate_snippet = |pattern: &str| {
macro_source.rfind("mut").expect("already checked this is a mutable reference") + "mut".len() #[allow(clippy::cast_possible_truncation)]
} else { macro_source.rfind(pattern).map(|pattern_pos| {
macro_source.rfind('&').expect("already checked this is a reference") + "&".len() let rpos = pattern_pos + pattern.len();
let span_after_ref = e.span.with_lo(BytePos(e.span.lo().0 + rpos as u32));
let span = trim_leading_whitespaces(span_after_ref);
snippet_with_applicability(cx, span, "_", &mut applicability)
})
}; };
#[allow(clippy::cast_possible_truncation)]
let span_after_ref = e.span.with_lo(BytePos(e.span.lo().0 + rpos as u32)); if *mutability == Mutability::Mut {
let span = trim_leading_whitespaces(span_after_ref); generate_snippet("mut")
snippet_with_applicability(cx, span, "_", &mut applicability) } else {
generate_snippet("&")
}
} else { } else {
snippet_with_applicability(cx, e.span, "_", &mut applicability) Some(snippet_with_applicability(cx, e.span, "_", &mut applicability))
} }
} else { } else {
snippet_with_applicability(cx, addrof_target.span, "_", &mut applicability) Some(snippet_with_applicability(cx, addrof_target.span, "_", &mut applicability))
}.to_string(); };
span_lint_and_sugg( if let Some(sugg) = sugg {
cx, span_lint_and_sugg(
DEREF_ADDROF, cx,
e.span, DEREF_ADDROF,
"immediately dereferencing a reference", e.span,
"try this", "immediately dereferencing a reference",
sugg, "try this",
applicability, sugg.to_string(),
); applicability,
);
}
} }
} }
} }

View File

@@ -0,0 +1,11 @@
fn cmark_check() {
let mut link_err = false;
macro_rules! cmark_error {
($bad:expr) => {
*$bad = true;
};
}
cmark_error!(&mut link_err);
}
pub fn main() {}