Skip to content

Commit 602bcf3

Browse files
committed
move get_hint_if_single_char_arg to methods/utils.rs
1 parent 27963c8 commit 602bcf3

File tree

5 files changed

+36
-35
lines changed

5 files changed

+36
-35
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,12 @@ 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 clippy_utils::{
6766
contains_return, get_trait_def_id, in_macro, iter_input_pats, match_qpath, method_calls, paths, return_ty,
6867
SpanlessEq,
6968
};
7069
use if_chain::if_chain;
71-
use rustc_ast::ast;
72-
use rustc_errors::Applicability;
7370
use rustc_hir as hir;
7471
use rustc_hir::{TraitItem, TraitItemKind};
7572
use rustc_lint::{LateContext, LateLintPass, LintContext};
@@ -1978,34 +1975,6 @@ fn lint_binary_expr_with_method_call(cx: &LateContext<'_>, info: &mut BinaryExpr
19781975
lint_with_both_lhs_and_rhs!(chars_last_cmp_with_unwrap::check, cx, info);
19791976
}
19801977

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

clippy_lints/src/methods/single_char_insert_string.rs

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 33 additions & 1 deletion
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)