Skip to content

Commit daab21e

Browse files
committed
Pulicize clippy_utils::ty::ty_from_hir_ty
And use it in the next commit to avoid ICE.
1 parent f09701a commit daab21e

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-15
lines changed

clippy_lints/src/zero_sized_map_values.rs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
2-
use clippy_utils::ty::{is_normalizable, is_type_diagnostic_item};
2+
use clippy_utils::ty::{is_normalizable, is_type_diagnostic_item, ty_from_hir_ty};
33
use rustc_hir::{self as hir, AmbigArg, HirId, ItemKind, Node};
4-
use rustc_hir_analysis::lower_ty;
54
use rustc_lint::{LateContext, LateLintPass};
65
use rustc_middle::ty::layout::LayoutOf as _;
7-
use rustc_middle::ty::{self, Ty, TypeVisitableExt};
6+
use rustc_middle::ty::{self, TypeVisitableExt};
87
use rustc_session::declare_lint_pass;
98
use rustc_span::sym;
109

@@ -82,15 +81,3 @@ fn in_trait_impl(cx: &LateContext<'_>, hir_id: HirId) -> bool {
8281
}
8382
false
8483
}
85-
86-
fn ty_from_hir_ty<'tcx>(cx: &LateContext<'tcx>, hir_ty: &hir::Ty<'tcx>) -> Ty<'tcx> {
87-
cx.maybe_typeck_results()
88-
.and_then(|results| {
89-
if results.hir_owner == hir_ty.hir_id.owner {
90-
results.node_type_opt(hir_ty.hir_id)
91-
} else {
92-
None
93-
}
94-
})
95-
.unwrap_or_else(|| lower_ty(cx.tcx, hir_ty))
96-
}

clippy_utils/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ extern crate rustc_data_structures;
3939
extern crate rustc_driver;
4040
extern crate rustc_errors;
4141
extern crate rustc_hir;
42+
extern crate rustc_hir_analysis;
4243
extern crate rustc_hir_typeck;
4344
extern crate rustc_index;
4445
extern crate rustc_infer;

clippy_utils/src/ty/mod.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use rustc_hir as hir;
1010
use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res};
1111
use rustc_hir::def_id::DefId;
1212
use rustc_hir::{Expr, FnDecl, LangItem, TyKind};
13+
use rustc_hir_analysis::lower_ty;
1314
use rustc_infer::infer::TyCtxtInferExt;
1415
use rustc_lint::LateContext;
1516
use rustc_middle::mir::ConstValue;
@@ -36,6 +37,19 @@ use crate::{def_path_def_ids, match_def_path, path_res};
3637
mod type_certainty;
3738
pub use type_certainty::expr_type_is_certain;
3839

40+
/// Lower a [`hir::Ty`] to a [`rustc_middle::Ty`].
41+
pub fn ty_from_hir_ty<'tcx>(cx: &LateContext<'tcx>, hir_ty: &hir::Ty<'tcx>) -> Ty<'tcx> {
42+
cx.maybe_typeck_results()
43+
.and_then(|results| {
44+
if results.hir_owner == hir_ty.hir_id.owner {
45+
results.node_type_opt(hir_ty.hir_id)
46+
} else {
47+
None
48+
}
49+
})
50+
.unwrap_or_else(|| lower_ty(cx.tcx, hir_ty))
51+
}
52+
3953
/// Checks if the given type implements copy.
4054
pub fn is_copy<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
4155
cx.type_is_copy_modulo_regions(ty)

0 commit comments

Comments
 (0)