Skip to content

Commit

Permalink
move get_hint_if_single_char_arg to methods/utils.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
TaKO8Ki committed Mar 17, 2021
1 parent 62fb578 commit c3d0476
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 35 deletions.
31 changes: 0 additions & 31 deletions clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,8 @@ mod zst_offset;

use bind_instead_of_map::BindInsteadOfMap;
use clippy_utils::diagnostics::{span_lint, span_lint_and_help};
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::ty::{contains_ty, implements_trait, is_copy, is_type_diagnostic_item};
use if_chain::if_chain;
use rustc_ast::ast;
use rustc_errors::Applicability;
use rustc_hir as hir;
use rustc_hir::{TraitItem, TraitItemKind};
use rustc_lint::{LateContext, LateLintPass, LintContext};
Expand Down Expand Up @@ -1979,34 +1976,6 @@ fn lint_binary_expr_with_method_call(cx: &LateContext<'_>, info: &mut BinaryExpr
lint_with_both_lhs_and_rhs!(chars_last_cmp_with_unwrap::check, cx, info);
}

fn get_hint_if_single_char_arg(
cx: &LateContext<'_>,
arg: &hir::Expr<'_>,
applicability: &mut Applicability,
) -> Option<String> {
if_chain! {
if let hir::ExprKind::Lit(lit) = &arg.kind;
if let ast::LitKind::Str(r, style) = lit.node;
let string = r.as_str();
if string.chars().count() == 1;
then {
let snip = snippet_with_applicability(cx, arg.span, &string, applicability);
let ch = if let ast::StrStyle::Raw(nhash) = style {
let nhash = nhash as usize;
// for raw string: r##"a"##
&snip[(nhash + 2)..(snip.len() - 1 - nhash)]
} else {
// for regular string: "a"
&snip[1..(snip.len() - 1)]
};
let hint = format!("'{}'", if ch == "'" { "\\'" } else { ch });
Some(hint)
} else {
None
}
}
}

const FN_HEADER: hir::FnHeader = hir::FnHeader {
unsafety: hir::Unsafety::Normal,
constness: hir::Constness::NotConst,
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/methods/single_char_insert_string.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::methods::get_hint_if_single_char_arg;
use crate::methods::utils::get_hint_if_single_char_arg;
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::source::snippet_with_applicability;
use rustc_errors::Applicability;
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/methods/single_char_pattern.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::methods::get_hint_if_single_char_arg;
use crate::methods::utils::get_hint_if_single_char_arg;
use clippy_utils::diagnostics::span_lint_and_sugg;
use if_chain::if_chain;
use rustc_errors::Applicability;
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/methods/single_char_push_string.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::methods::get_hint_if_single_char_arg;
use crate::methods::utils::get_hint_if_single_char_arg;
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::source::snippet_with_applicability;
use rustc_errors::Applicability;
Expand Down
34 changes: 33 additions & 1 deletion clippy_lints/src/methods/utils.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::ty::is_type_diagnostic_item;
use if_chain::if_chain;
use rustc_ast::ast;
use rustc_errors::Applicability;
use rustc_hir as hir;
use rustc_lint::LateContext;
use rustc_middle::ty;
use rustc_middle::ty::Ty;
use rustc_span::symbol::sym;

pub fn derefs_to_slice<'tcx>(
pub(super) fn derefs_to_slice<'tcx>(
cx: &LateContext<'tcx>,
expr: &'tcx hir::Expr<'tcx>,
ty: Ty<'tcx>,
Expand Down Expand Up @@ -44,3 +48,31 @@ pub fn derefs_to_slice<'tcx>(
}
}
}

pub(super) fn get_hint_if_single_char_arg(
cx: &LateContext<'_>,
arg: &hir::Expr<'_>,
applicability: &mut Applicability,
) -> Option<String> {
if_chain! {
if let hir::ExprKind::Lit(lit) = &arg.kind;
if let ast::LitKind::Str(r, style) = lit.node;
let string = r.as_str();
if string.chars().count() == 1;
then {
let snip = snippet_with_applicability(cx, arg.span, &string, applicability);
let ch = if let ast::StrStyle::Raw(nhash) = style {
let nhash = nhash as usize;
// for raw string: r##"a"##
&snip[(nhash + 2)..(snip.len() - 1 - nhash)]
} else {
// for regular string: "a"
&snip[1..(snip.len() - 1)]
};
let hint = format!("'{}'", if ch == "'" { "\\'" } else { ch });
Some(hint)
} else {
None
}
}
}

0 comments on commit c3d0476

Please sign in to comment.