Skip to content

Commit bf67fcf

Browse files
committed
Move is_normalizable into utils
1 parent 20318e0 commit bf67fcf

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

clippy_lints/src/transmute.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use crate::utils::{last_path_segment, match_def_path, paths, snippet, span_lint, span_lint_and_then, sugg};
1+
use crate::utils::{
2+
is_normalizable, last_path_segment, match_def_path, paths, snippet, span_lint, span_lint_and_then, sugg,
3+
};
24
use if_chain::if_chain;
35
use rustc::declare_lint_pass;
46
use rustc::hir::*;
@@ -641,12 +643,7 @@ fn get_type_snippet(cx: &LateContext<'_, '_>, path: &QPath<'_>, to_ref_ty: Ty<'_
641643
fn is_layout_incompatible<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, from: Ty<'tcx>, to: Ty<'tcx>) -> bool {
642644
let empty_param_env = ty::ParamEnv::empty();
643645
// check if `from` and `to` are normalizable to avoid ICE (#4968)
644-
let is_normalizable = cx.tcx.infer_ctxt().enter(|infcx| {
645-
let cause = rustc::traits::ObligationCause::dummy();
646-
infcx.at(&cause, empty_param_env).normalize(&from).is_ok()
647-
&& infcx.at(&cause, empty_param_env).normalize(&to).is_ok()
648-
});
649-
if !is_normalizable {
646+
if !(is_normalizable(cx, empty_param_env, from) && is_normalizable(cx, empty_param_env, to)) {
650647
return false;
651648
}
652649
let from_ty_layout = cx.tcx.layout_of(empty_param_env.and(from));

clippy_lints/src/utils/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,6 +1111,15 @@ pub fn match_function_call<'a, 'tcx>(
11111111
None
11121112
}
11131113

1114+
/// Checks if `Ty` is normalizable. This function is useful
1115+
/// to avoid crashes on `layout_of`.
1116+
pub fn is_normalizable<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, param_env: ty::ParamEnv<'tcx>, ty: Ty<'tcx>) -> bool {
1117+
cx.tcx.infer_ctxt().enter(|infcx| {
1118+
let cause = rustc::traits::ObligationCause::dummy();
1119+
infcx.at(&cause, param_env).normalize(&ty).is_ok()
1120+
})
1121+
}
1122+
11141123
#[cfg(test)]
11151124
mod test {
11161125
use super::{trim_multiline, without_block_comments};

0 commit comments

Comments
 (0)