|
1 | | -use clippy_utils::diagnostics::span_lint_and_sugg; |
2 | | -use clippy_utils::source::snippet; |
| 1 | +use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_help}; |
| 2 | +use clippy_utils::source::snippet_with_applicability; |
3 | 3 | use clippy_utils::ty::is_type_diagnostic_item; |
4 | 4 | use clippy_utils::macros::{root_macro_call, FormatArgsExpn}; |
5 | 5 | use rustc_errors::Applicability; |
6 | 6 | use rustc_hir::{Expr, ExprKind}; |
7 | 7 | use rustc_lint::{LateContext, LateLintPass}; |
8 | 8 | use rustc_session::{declare_lint_pass, declare_tool_lint}; |
9 | 9 | use rustc_span::sym; |
10 | | -use std::fmt::Write as _; |
11 | 10 |
|
12 | 11 | declare_clippy_lint! { |
13 | 12 | /// ### What it does |
@@ -46,55 +45,53 @@ impl<'tcx> LateLintPass<'tcx> for PathFromFormat { |
46 | 45 | if let Some(macro_call) = root_macro_call(args[0].span); |
47 | 46 | if cx.tcx.item_name(macro_call.def_id) == sym::format; |
48 | 47 | if let Some(format_args) = FormatArgsExpn::find_nested(cx, &args[0], macro_call.expn); |
| 48 | + let mut applicability = Applicability::MachineApplicable; |
| 49 | + let format_args_snip = snippet_with_applicability(cx, format_args.inputs_span(), "..", &mut applicability); |
| 50 | + if let Some(end_of_literal) = format_args_snip.find("\","); |
49 | 51 | then { |
50 | | - let full_expr = snippet(cx, expr.span, "error").to_string(); |
51 | | - let split_expr: Vec<&str> = full_expr.split('!').collect(); |
52 | | - let args_to_macro = split_expr[1]; |
53 | | - let replaced = args_to_macro.replace('(', "").replace(')', ""); |
54 | | - let unformatted: Vec<&str> = replaced.split(',').collect(); |
55 | | - let mut push_targets: Vec<String> = Vec::new(); |
56 | | - let mut temp_string = String::new(); |
57 | | - for c in unformatted[0].chars() { |
58 | | - if c == '/' || c == '\\' { |
59 | | - push_targets.push(temp_string.clone()); |
60 | | - temp_string = String::new(); |
61 | | - } |
62 | | - else if c == '}' { |
63 | | - temp_string.push_str(&unformatted[1].replace(' ', "")); |
64 | | - } |
65 | | - else if c != '{' && c != '"' { |
66 | | - temp_string.push(c); |
67 | | - } |
68 | | - } |
69 | | - if !temp_string.is_empty() { |
70 | | - push_targets.push(temp_string.clone()); |
71 | | - temp_string = String::new(); |
| 52 | + let (literal, vars) = format_args_snip.split_at(end_of_literal); |
| 53 | + let mut literal = literal.to_string(); |
| 54 | + literal.remove(0); |
| 55 | + let v: Vec<&str> = literal.split("{}").collect(); |
| 56 | + let real_vars = vars.strip_prefix("\", ").unwrap_or(vars); |
| 57 | + if v.len() != 2 || real_vars.contains(',') { |
| 58 | + span_lint_and_help( |
| 59 | + cx, |
| 60 | + PATH_FROM_FORMAT, |
| 61 | + expr.span, |
| 62 | + "`format!(..)` used to form `PathBuf`", |
| 63 | + None, |
| 64 | + "consider using `.join()` to avoid the extra allocation", |
| 65 | + ); |
| 66 | + return; |
72 | 67 | } |
73 | | - for target in push_targets { |
74 | | - let target_processed = |
75 | | - if target == unformatted[1].replace(' ', "") { |
76 | | - target |
| 68 | + let sugg = { |
| 69 | + if v[0].is_empty() { |
| 70 | + let mut str1 = v[1].to_string(); |
| 71 | + if str1.starts_with('\\') || str1.starts_with('/') { |
| 72 | + str1.remove(0); |
77 | 73 | } |
78 | | - else { |
79 | | - let mut s = String::from("\""); |
80 | | - s.push_str(&target); |
81 | | - s.push('"'); |
82 | | - s |
83 | | - }; |
84 | | - if temp_string.is_empty() { |
85 | | - let _ = write!(temp_string, "Path::new({})", target_processed); |
| 74 | + format!("Path::new({real_vars}).join(\"{str1}\")") |
| 75 | + } |
| 76 | + else if v[1].is_empty() { |
| 77 | + let str1 = v[0].to_string(); |
| 78 | + format!("Path::new(\"{str1}\").join({real_vars})") |
86 | 79 | } |
87 | 80 | else { |
88 | | - let _ = write!(temp_string, ".join({})", target_processed); |
| 81 | + let (str1, mut str2) = (v[0].to_string(), v[1].to_string()); |
| 82 | + if str2.starts_with('\\') || str2.starts_with('/') { |
| 83 | + str2.remove(0); |
| 84 | + } |
| 85 | + format!("Path::new(\"{str1}\").join({real_vars}).join(\"{str2}\")") |
89 | 86 | } |
90 | | - } |
| 87 | + }; |
91 | 88 | span_lint_and_sugg( |
92 | 89 | cx, |
93 | 90 | PATH_FROM_FORMAT, |
94 | 91 | expr.span, |
95 | 92 | "`format!(..)` used to form `PathBuf`", |
96 | 93 | "consider using `.join()` to avoid the extra allocation", |
97 | | - temp_string, |
| 94 | + sugg, |
98 | 95 | Applicability::MaybeIncorrect, |
99 | 96 | ); |
100 | 97 | } |
|
0 commit comments