|
1 | 1 | use clippy_utils::diagnostics::span_lint_and_sugg; |
2 | | -use clippy_utils::paths; |
3 | | -use clippy_utils::source::snippet_with_macro_callsite; |
4 | | -use clippy_utils::ty::{is_type_diagnostic_item, is_type_ref_to_diagnostic_item}; |
| 2 | +use clippy_utils::source::snippet_with_context; |
| 3 | +use clippy_utils::ty::is_type_diagnostic_item; |
| 4 | +use clippy_utils::visitors::is_expr_unsafe; |
| 5 | +use clippy_utils::{get_parent_node, match_libc_symbol}; |
5 | 6 | use if_chain::if_chain; |
6 | 7 | use rustc_errors::Applicability; |
7 | | -use rustc_hir as hir; |
| 8 | +use rustc_hir::{Block, BlockCheckMode, Expr, ExprKind, Node, UnsafeSource}; |
8 | 9 | use rustc_lint::{LateContext, LateLintPass}; |
9 | 10 | use rustc_session::{declare_lint_pass, declare_tool_lint}; |
10 | | -use rustc_span::symbol::{sym, Symbol}; |
| 11 | +use rustc_span::symbol::sym; |
11 | 12 |
|
12 | 13 | declare_clippy_lint! { |
13 | 14 | /// ### What it does |
@@ -39,41 +40,47 @@ declare_clippy_lint! { |
39 | 40 | declare_lint_pass!(StrlenOnCStrings => [STRLEN_ON_C_STRINGS]); |
40 | 41 |
|
41 | 42 | impl LateLintPass<'tcx> for StrlenOnCStrings { |
42 | | - fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) { |
43 | | - if expr.span.from_expansion() { |
44 | | - return; |
45 | | - } |
46 | | - |
| 43 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { |
47 | 44 | if_chain! { |
48 | | - if let hir::ExprKind::Call(func, [recv]) = expr.kind; |
49 | | - if let hir::ExprKind::Path(hir::QPath::Resolved(_, path)) = func.kind; |
50 | | - |
51 | | - if (&paths::LIBC_STRLEN).iter().map(|x| Symbol::intern(x)).eq( |
52 | | - path.segments.iter().map(|seg| seg.ident.name)); |
53 | | - if let hir::ExprKind::MethodCall(path, _, args, _) = recv.kind; |
54 | | - if args.len() == 1; |
55 | | - if !args.iter().any(|e| e.span.from_expansion()); |
| 45 | + if !expr.span.from_expansion(); |
| 46 | + if let ExprKind::Call(func, [recv]) = expr.kind; |
| 47 | + if let ExprKind::Path(path) = &func.kind; |
| 48 | + if let Some(did) = cx.qpath_res(path, func.hir_id).opt_def_id(); |
| 49 | + if match_libc_symbol(cx, did, "strlen"); |
| 50 | + if let ExprKind::MethodCall(path, _, [self_arg], _) = recv.kind; |
| 51 | + if !recv.span.from_expansion(); |
56 | 52 | if path.ident.name == sym::as_ptr; |
57 | 53 | then { |
58 | | - let cstring = &args[0]; |
59 | | - let ty = cx.typeck_results().expr_ty(cstring); |
60 | | - let val_name = snippet_with_macro_callsite(cx, cstring.span, ".."); |
61 | | - let sugg = if is_type_diagnostic_item(cx, ty, sym::cstring_type){ |
62 | | - format!("{}.as_bytes().len()", val_name) |
63 | | - } else if is_type_ref_to_diagnostic_item(cx, ty, sym::CStr){ |
64 | | - format!("{}.to_bytes().len()", val_name) |
| 54 | + let ctxt = expr.span.ctxt(); |
| 55 | + let span = match get_parent_node(cx.tcx, expr.hir_id) { |
| 56 | + Some(Node::Block(&Block { |
| 57 | + rules: BlockCheckMode::UnsafeBlock(UnsafeSource::UserProvided), span, .. |
| 58 | + })) |
| 59 | + if span.ctxt() == ctxt && !is_expr_unsafe(cx, self_arg) => { |
| 60 | + span |
| 61 | + } |
| 62 | + _ => expr.span, |
| 63 | + }; |
| 64 | + |
| 65 | + let ty = cx.typeck_results().expr_ty(self_arg).peel_refs(); |
| 66 | + let mut app = Applicability::MachineApplicable; |
| 67 | + let val_name = snippet_with_context(cx, self_arg.span, ctxt, "..", &mut app).0; |
| 68 | + let method_name = if is_type_diagnostic_item(cx, ty, sym::cstring_type) { |
| 69 | + "as_bytes" |
| 70 | + } else if is_type_diagnostic_item(cx, ty, sym::CStr) { |
| 71 | + "to_bytes" |
65 | 72 | } else { |
66 | 73 | return; |
67 | 74 | }; |
68 | 75 |
|
69 | 76 | span_lint_and_sugg( |
70 | 77 | cx, |
71 | 78 | STRLEN_ON_C_STRINGS, |
72 | | - expr.span, |
| 79 | + span, |
73 | 80 | "using `libc::strlen` on a `CString` or `CStr` value", |
74 | | - "try this (you might also need to get rid of `unsafe` block in some cases):", |
75 | | - sugg, |
76 | | - Applicability::Unspecified // Sometimes unnecessary `unsafe` block |
| 81 | + "try this", |
| 82 | + format!("{}.{}().len()", val_name, method_name), |
| 83 | + app, |
77 | 84 | ); |
78 | 85 | } |
79 | 86 | } |
|
0 commit comments