Skip to content

Commit c3d0476

Browse files
committed
move get_hint_if_single_char_arg to methods/utils.rs
1 parent 62fb578 commit c3d0476

File tree

5 files changed

+36
-35
lines changed

5 files changed

+36
-35
lines changed

clippy_lints/src/methods/mod.rs

-31
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,8 @@ mod zst_offset;
6161

6262
use bind_instead_of_map::BindInsteadOfMap;
6363
use clippy_utils::diagnostics::{span_lint, span_lint_and_help};
64-
use clippy_utils::source::snippet_with_applicability;
6564
use clippy_utils::ty::{contains_ty, implements_trait, is_copy, is_type_diagnostic_item};
6665
use if_chain::if_chain;
67-
use rustc_ast::ast;
68-
use rustc_errors::Applicability;
6966
use rustc_hir as hir;
7067
use rustc_hir::{TraitItem, TraitItemKind};
7168
use rustc_lint::{LateContext, LateLintPass, LintContext};
@@ -1979,34 +1976,6 @@ fn lint_binary_expr_with_method_call(cx: &LateContext<'_>, info: &mut BinaryExpr
19791976
lint_with_both_lhs_and_rhs!(chars_last_cmp_with_unwrap::check, cx, info);
19801977
}
19811978

1982-
fn get_hint_if_single_char_arg(
1983-
cx: &LateContext<'_>,
1984-
arg: &hir::Expr<'_>,
1985-
applicability: &mut Applicability,
1986-
) -> Option<String> {
1987-
if_chain! {
1988-
if let hir::ExprKind::Lit(lit) = &arg.kind;
1989-
if let ast::LitKind::Str(r, style) = lit.node;
1990-
let string = r.as_str();
1991-
if string.chars().count() == 1;
1992-
then {
1993-
let snip = snippet_with_applicability(cx, arg.span, &string, applicability);
1994-
let ch = if let ast::StrStyle::Raw(nhash) = style {
1995-
let nhash = nhash as usize;
1996-
// for raw string: r##"a"##
1997-
&snip[(nhash + 2)..(snip.len() - 1 - nhash)]
1998-
} else {
1999-
// for regular string: "a"
2000-
&snip[1..(snip.len() - 1)]
2001-
};
2002-
let hint = format!("'{}'", if ch == "'" { "\\'" } else { ch });
2003-
Some(hint)
2004-
} else {
2005-
None
2006-
}
2007-
}
2008-
}
2009-
20101979
const FN_HEADER: hir::FnHeader = hir::FnHeader {
20111980
unsafety: hir::Unsafety::Normal,
20121981
constness: hir::Constness::NotConst,

clippy_lints/src/methods/single_char_insert_string.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::methods::get_hint_if_single_char_arg;
1+
use crate::methods::utils::get_hint_if_single_char_arg;
22
use clippy_utils::diagnostics::span_lint_and_sugg;
33
use clippy_utils::source::snippet_with_applicability;
44
use rustc_errors::Applicability;

clippy_lints/src/methods/single_char_pattern.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::methods::get_hint_if_single_char_arg;
1+
use crate::methods::utils::get_hint_if_single_char_arg;
22
use clippy_utils::diagnostics::span_lint_and_sugg;
33
use if_chain::if_chain;
44
use rustc_errors::Applicability;

clippy_lints/src/methods/single_char_push_string.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::methods::get_hint_if_single_char_arg;
1+
use crate::methods::utils::get_hint_if_single_char_arg;
22
use clippy_utils::diagnostics::span_lint_and_sugg;
33
use clippy_utils::source::snippet_with_applicability;
44
use rustc_errors::Applicability;

clippy_lints/src/methods/utils.rs

+33-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1+
use clippy_utils::source::snippet_with_applicability;
12
use clippy_utils::ty::is_type_diagnostic_item;
3+
use if_chain::if_chain;
4+
use rustc_ast::ast;
5+
use rustc_errors::Applicability;
26
use rustc_hir as hir;
37
use rustc_lint::LateContext;
48
use rustc_middle::ty;
59
use rustc_middle::ty::Ty;
610
use rustc_span::symbol::sym;
711

8-
pub fn derefs_to_slice<'tcx>(
12+
pub(super) fn derefs_to_slice<'tcx>(
913
cx: &LateContext<'tcx>,
1014
expr: &'tcx hir::Expr<'tcx>,
1115
ty: Ty<'tcx>,
@@ -44,3 +48,31 @@ pub fn derefs_to_slice<'tcx>(
4448
}
4549
}
4650
}
51+
52+
pub(super) fn get_hint_if_single_char_arg(
53+
cx: &LateContext<'_>,
54+
arg: &hir::Expr<'_>,
55+
applicability: &mut Applicability,
56+
) -> Option<String> {
57+
if_chain! {
58+
if let hir::ExprKind::Lit(lit) = &arg.kind;
59+
if let ast::LitKind::Str(r, style) = lit.node;
60+
let string = r.as_str();
61+
if string.chars().count() == 1;
62+
then {
63+
let snip = snippet_with_applicability(cx, arg.span, &string, applicability);
64+
let ch = if let ast::StrStyle::Raw(nhash) = style {
65+
let nhash = nhash as usize;
66+
// for raw string: r##"a"##
67+
&snip[(nhash + 2)..(snip.len() - 1 - nhash)]
68+
} else {
69+
// for regular string: "a"
70+
&snip[1..(snip.len() - 1)]
71+
};
72+
let hint = format!("'{}'", if ch == "'" { "\\'" } else { ch });
73+
Some(hint)
74+
} else {
75+
None
76+
}
77+
}
78+
}

0 commit comments

Comments
 (0)