2021-03-25 13:29:11 -05:00
|
|
|
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::{self, Ty};
|
|
|
|
use rustc_span::symbol::sym;
|
|
|
|
|
|
|
|
pub(super) fn derefs_to_slice<'tcx>(
|
|
|
|
cx: &LateContext<'tcx>,
|
|
|
|
expr: &'tcx hir::Expr<'tcx>,
|
|
|
|
ty: Ty<'tcx>,
|
|
|
|
) -> Option<&'tcx hir::Expr<'tcx>> {
|
|
|
|
fn may_slice<'a>(cx: &LateContext<'a>, ty: Ty<'a>) -> bool {
|
|
|
|
match ty.kind() {
|
|
|
|
ty::Slice(_) => true,
|
|
|
|
ty::Adt(def, _) if def.is_box() => may_slice(cx, ty.boxed_ty()),
|
2021-10-02 18:51:01 -05:00
|
|
|
ty::Adt(..) => is_type_diagnostic_item(cx, ty, sym::Vec),
|
2021-04-27 09:55:11 -05:00
|
|
|
ty::Array(_, size) => size.try_eval_usize(cx.tcx, cx.param_env).is_some(),
|
2021-03-25 13:29:11 -05:00
|
|
|
ty::Ref(_, inner, _) => may_slice(cx, inner),
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-01 11:17:50 -06:00
|
|
|
if let hir::ExprKind::MethodCall(path, [self_arg, ..], _) = &expr.kind {
|
2021-09-08 09:31:47 -05:00
|
|
|
if path.ident.name == sym::iter && may_slice(cx, cx.typeck_results().expr_ty(self_arg)) {
|
|
|
|
Some(self_arg)
|
2021-03-25 13:29:11 -05:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
match ty.kind() {
|
|
|
|
ty::Slice(_) => Some(expr),
|
|
|
|
ty::Adt(def, _) if def.is_box() && may_slice(cx, ty.boxed_ty()) => Some(expr),
|
|
|
|
ty::Ref(_, inner, _) => {
|
|
|
|
if may_slice(cx, inner) {
|
|
|
|
Some(expr)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2021-12-30 08:10:43 -06:00
|
|
|
let snip = snippet_with_applicability(cx, arg.span, string, applicability);
|
2021-03-25 13:29:11 -05:00
|
|
|
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)]
|
|
|
|
};
|
2021-12-06 05:33:31 -06:00
|
|
|
|
|
|
|
let hint = format!("'{}'", match ch {
|
|
|
|
"'" => "\\'" ,
|
|
|
|
r"\" => "\\\\",
|
|
|
|
_ => ch,
|
|
|
|
});
|
|
|
|
|
2021-03-25 13:29:11 -05:00
|
|
|
Some(hint)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|