Skip to content

Commit

Permalink
first two suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
merelymyself committed Jun 4, 2022
1 parent 3fbe0f3 commit e897196
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 5 deletions.
6 changes: 4 additions & 2 deletions clippy_lints/src/path_from_format.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::source::snippet;
use clippy_utils::ty::is_type_diagnostic_item;
use clippy_utils::macros::{root_macro_call, FormatArgsExpn};
use rustc_errors::Applicability;
use rustc_hir::{Expr, ExprKind};
use rustc_lint::{LateContext, LateLintPass};
Expand Down Expand Up @@ -42,8 +43,9 @@ impl<'tcx> LateLintPass<'tcx> for PathFromFormat {
if let ty = cx.typeck_results().expr_ty(expr);
if is_type_diagnostic_item(cx, ty, sym::PathBuf);
if !args.is_empty();
if let Some(macro_def_id) = args[0].span.ctxt().outer_expn_data().macro_def_id;
if cx.tcx.get_diagnostic_name(macro_def_id) == Some(sym::format_macro);
if let Some(macro_call) = root_macro_call(args[0].span);
if cx.tcx.item_name(macro_call.def_id) == sym::format;
if let Some(format_args) = FormatArgsExpn::find_nested(cx, &args[0], macro_call.expn);
then {
let full_expr = snippet(cx, expr.span, "error").to_string();
let split_expr: Vec<&str> = full_expr.split('!').collect();
Expand Down
2 changes: 0 additions & 2 deletions tests/ui/path_from_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
use std::path::PathBuf;

fn main() {
// let base_path = "";
// PathBuf::from(format!("{}/foo/bar", base_path));
let mut base_path1 = "";
PathBuf::from(format!("{}/foo/bar", base_path1));
}
2 changes: 1 addition & 1 deletion tests/ui/path_from_format.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: `format!(..)` used to form `PathBuf`
--> $DIR/path_from_format.rs:9:5
--> $DIR/path_from_format.rs:7:5
|
LL | PathBuf::from(format!("{}/foo/bar", base_path1));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `.join()` to avoid the extra allocation: `Path::new(base_path1).join("foo").join("bar")`
Expand Down

0 comments on commit e897196

Please sign in to comment.