Skip to content

Commit f59d7af

Browse files
committed
first two suggestions
1 parent 7fbd14f commit f59d7af

File tree

3 files changed

+5
-5
lines changed

3 files changed

+5
-5
lines changed

clippy_lints/src/path_from_format.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::source::snippet;
33
use clippy_utils::ty::is_type_diagnostic_item;
4+
use clippy_utils::macros::{root_macro_call, FormatArgsExpn};
45
use rustc_errors::Applicability;
56
use rustc_hir::{Expr, ExprKind};
67
use rustc_lint::{LateContext, LateLintPass};
@@ -42,8 +43,9 @@ impl<'tcx> LateLintPass<'tcx> for PathFromFormat {
4243
if let ty = cx.typeck_results().expr_ty(expr);
4344
if is_type_diagnostic_item(cx, ty, sym::PathBuf);
4445
if !args.is_empty();
45-
if let Some(macro_def_id) = args[0].span.ctxt().outer_expn_data().macro_def_id;
46-
if cx.tcx.get_diagnostic_name(macro_def_id) == Some(sym::format_macro);
46+
if let Some(macro_call) = root_macro_call(args[0].span);
47+
if cx.tcx.item_name(macro_call.def_id) == sym::format;
48+
if let Some(format_args) = FormatArgsExpn::find_nested(cx, &args[0], macro_call.expn);
4749
then {
4850
let full_expr = snippet(cx, expr.span, "error").to_string();
4951
let split_expr: Vec<&str> = full_expr.split('!').collect();

tests/ui/path_from_format.rs

-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
use std::path::PathBuf;
44

55
fn main() {
6-
// let base_path = "";
7-
// PathBuf::from(format!("{}/foo/bar", base_path));
86
let mut base_path1 = "";
97
PathBuf::from(format!("{}/foo/bar", base_path1));
108
}

tests/ui/path_from_format.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: `format!(..)` used to form `PathBuf`
2-
--> $DIR/path_from_format.rs:9:5
2+
--> $DIR/path_from_format.rs:7:5
33
|
44
LL | PathBuf::from(format!("{}/foo/bar", base_path1));
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `.join()` to avoid the extra allocation: `Path::new(base_path1).join("foo").join("bar")`

0 commit comments

Comments
 (0)