Skip to content

Commit bdb13cd

Browse files
committed
refactor: move has_debug_impl to clippy_utils::ty
1 parent cd69d86 commit bdb13cd

File tree

4 files changed

+12
-26
lines changed

4 files changed

+12
-26
lines changed

clippy_lints/src/assertions_on_result_states.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::macros::{find_assert_args, root_macro_call_first_node, PanicExpn};
33
use clippy_utils::source::snippet_with_context;
4-
use clippy_utils::ty::{implements_trait, is_copy, is_type_diagnostic_item};
4+
use clippy_utils::ty::{has_debug_impl, is_copy, is_type_diagnostic_item};
55
use clippy_utils::usage::local_used_after_expr;
66
use clippy_utils::{is_expr_final_block_expr, path_res};
77
use rustc_errors::Applicability;
@@ -97,13 +97,6 @@ impl<'tcx> LateLintPass<'tcx> for AssertionsOnResultStates {
9797
}
9898
}
9999

100-
/// This checks whether a given type is known to implement Debug.
101-
fn has_debug_impl<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
102-
cx.tcx
103-
.get_diagnostic_item(sym::Debug)
104-
.map_or(false, |debug| implements_trait(cx, ty, debug, &[]))
105-
}
106-
107100
fn type_suitable_to_unwrap<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
108101
has_debug_impl(cx, ty) && !ty.is_unit() && !ty.is_never()
109102
}

clippy_lints/src/methods/err_expect.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::ERR_EXPECT;
22
use clippy_utils::diagnostics::span_lint_and_sugg;
3-
use clippy_utils::ty::implements_trait;
3+
use clippy_utils::ty::has_debug_impl;
44
use clippy_utils::{meets_msrv, msrvs, ty::is_type_diagnostic_item};
55
use rustc_errors::Applicability;
66
use rustc_lint::LateContext;
@@ -28,7 +28,7 @@ pub(super) fn check(
2828
// Tests if the T type in a `Result<T, E>` is not None
2929
if let Some(data_type) = get_data_type(cx, result_type);
3030
// Tests if the T type in a `Result<T, E>` implements debug
31-
if has_debug_impl(data_type, cx);
31+
if has_debug_impl(cx, data_type);
3232

3333
then {
3434
span_lint_and_sugg(
@@ -51,10 +51,3 @@ fn get_data_type<'a>(cx: &LateContext<'_>, ty: Ty<'a>) -> Option<Ty<'a>> {
5151
_ => None,
5252
}
5353
}
54-
55-
/// Given a type, very if the Debug trait has been impl'd
56-
fn has_debug_impl<'tcx>(ty: Ty<'tcx>, cx: &LateContext<'tcx>) -> bool {
57-
cx.tcx
58-
.get_diagnostic_item(sym::Debug)
59-
.map_or(false, |debug| implements_trait(cx, ty, debug, &[]))
60-
}

clippy_lints/src/methods/ok_expect.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
2-
use clippy_utils::ty::{implements_trait, is_type_diagnostic_item};
2+
use clippy_utils::ty::{has_debug_impl, is_type_diagnostic_item};
33
use if_chain::if_chain;
44
use rustc_hir as hir;
55
use rustc_lint::LateContext;
@@ -15,7 +15,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr
1515
if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(recv), sym::Result);
1616
let result_type = cx.typeck_results().expr_ty(recv);
1717
if let Some(error_type) = get_error_type(cx, result_type);
18-
if has_debug_impl(error_type, cx);
18+
if has_debug_impl(cx, error_type);
1919

2020
then {
2121
span_lint_and_help(
@@ -37,10 +37,3 @@ fn get_error_type<'a>(cx: &LateContext<'_>, ty: Ty<'a>) -> Option<Ty<'a>> {
3737
_ => None,
3838
}
3939
}
40-
41-
/// This checks whether a given type is known to implement Debug.
42-
fn has_debug_impl<'tcx>(ty: Ty<'tcx>, cx: &LateContext<'tcx>) -> bool {
43-
cx.tcx
44-
.get_diagnostic_item(sym::Debug)
45-
.map_or(false, |debug| implements_trait(cx, ty, debug, &[]))
46-
}

clippy_utils/src/ty.rs

+7
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ pub fn is_copy<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
3131
ty.is_copy_modulo_regions(cx.tcx.at(DUMMY_SP), cx.param_env)
3232
}
3333

34+
/// This checks whether a given type is known to implement Debug.
35+
pub fn has_debug_impl<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
36+
cx.tcx
37+
.get_diagnostic_item(sym::Debug)
38+
.map_or(false, |debug| implements_trait(cx, ty, debug, &[]))
39+
}
40+
3441
/// Checks whether a type can be partially moved.
3542
pub fn can_partially_move_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
3643
if has_drop(cx, ty) || is_copy(cx, ty) {

0 commit comments

Comments
 (0)