-
Notifications
You must be signed in to change notification settings - Fork 48
Add format_concat_args lint to optimize string formatting #1638
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
4e8e2fc
ab1185c
e69fce9
e3d7e31
6c7ab32
5ed0d77
5aded76
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| [target.'cfg(all())'] | ||
| rustflags = ["-C", "linker=dylint-link"] | ||
|
|
||
| # For Rust versions 1.74.0 and onward, the following alternative can be used | ||
| # (see https://github.com/rust-lang/cargo/pull/12535): | ||
| # linker = "dylint-link" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| /target | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This file can be removed. A |
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,26 @@ | ||||||
| [package] | ||||||
| name = "format_concat_args" | ||||||
| version = "0.1.0" | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
To match the versions used elsewhere in this repository. |
||||||
| authors = ["Your Name <[email protected]>"] | ||||||
| description = "A Dylint lint to suggest using `concat!(...)` instead of `format!(...)` for all-constant Display arguments." | ||||||
| edition = "2021" | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| publish = false | ||||||
|
|
||||||
| [lib] | ||||||
| crate-type = ["cdylib", "rlib"] | ||||||
|
|
||||||
| [dependencies] | ||||||
| clippy_utils = { git = "https://github.com/rust-lang/rust-clippy", rev = "7bb54d91be1af212faaa078786c1d2271a67d4f9" } | ||||||
| dylint_linting = { path = "../../../utils/linting" } | ||||||
|
|
||||||
| [dev-dependencies] | ||||||
| dylint_testing = { path = "../../../utils/testing" } | ||||||
|
|
||||||
| [features] | ||||||
| rlib = ["dylint_linting/constituent"] | ||||||
|
|
||||||
| [lints] | ||||||
| workspace = true | ||||||
|
|
||||||
| [package.metadata.rust-analyzer] | ||||||
| rustc_private = true | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| # format_concat_args | ||
|
|
||
|
|
||
| ## Examples | ||
|
|
||
|
|
||
| ## Building and Testing | ||
|
|
||
| ## Limitations | ||
|
|
||
| ## References | ||
|
|
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,3 @@ | ||||||
| [toolchain] | ||||||
| channel = "nightly-2025-02-20" | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There's a test to ensure that all examples use the same toolchain, and this is the toolchain they currently use. Also, I think this is the toolchain that goes with the version of |
||||||
| components = ["llvm-tools-preview", "rustc-dev"] | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| #![feature(rustc_private)] | ||
|
|
||
| extern crate rustc_ast; | ||
| extern crate rustc_errors; | ||
| extern crate rustc_hir; | ||
| extern crate rustc_lint; | ||
| extern crate rustc_middle; | ||
| extern crate rustc_session; | ||
| extern crate rustc_span; | ||
|
|
||
| #[cfg(not(feature = "rlib"))] | ||
| dylint_linting::dylint_library!(); | ||
|
|
||
| use clippy_utils::{ | ||
| diagnostics::span_lint_and_sugg, | ||
| is_expn_of, | ||
| }; | ||
| use rustc_hir::Expr; | ||
| use rustc_lint::{Lint, LateContext, LateLintPass, Level}; | ||
| use rustc_session::declare_lint_pass; | ||
| use rustc_span::sym; | ||
|
|
||
| // Declare the lint directly | ||
| pub static FORMAT_CONCAT_ARGS: &Lint = &Lint { | ||
| name: "format_concat_args", | ||
| default_level: Level::Allow, | ||
| desc: "Checks for `format!(...)` invocations where `concat!(...)` could be used instead.", | ||
| edition_lint_opts: None, | ||
| report_in_external_macro: true, | ||
| future_incompatible: None, | ||
| is_externally_loaded: false, | ||
| eval_always: false, | ||
| feature_gate: None, | ||
| crate_level_only: false, | ||
| }; | ||
|
|
||
| // Declare the lint pass | ||
| declare_lint_pass!(FormatConcatArgs => [FORMAT_CONCAT_ARGS]); | ||
|
|
||
| impl<'tcx> LateLintPass<'tcx> for FormatConcatArgs { | ||
| fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { | ||
| // Check if the expression is a `format!` macro invocation | ||
| if is_expn_of(expr.span, sym::format_args.as_str()).is_some() { | ||
| let expn_data = expr.span.ctxt().outer_expn_data(); | ||
|
|
||
| // Ensure this is from `std::format!` specifically | ||
| if expn_data.macro_def_id != cx.tcx.get_diagnostic_item(sym::format_macro) { | ||
| return; // Not a std::format! macro call | ||
| } | ||
|
|
||
| span_lint_and_sugg( | ||
| cx, | ||
| FORMAT_CONCAT_ARGS, | ||
| expr.span, | ||
| "this `format!(...)` invocation might be replaceable with `concat!(...)`", | ||
| "consider using concat! if all arguments are constant", | ||
| "concat!(...)".to_string(), | ||
| rustc_errors::Applicability::HasPlaceholders, | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(not(feature = "rlib"))] | ||
| #[allow(unused_extern_crates)] | ||
| #[allow(clippy::float_arithmetic, clippy::option_option, clippy::unreachable)] | ||
| fn main() { | ||
| dylint_linting::test(env!("CARGO_PKG_NAME"), &[]); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| // for testing, | ||
| fn main() { | ||
| // Should trigger the lint | ||
| let _s1 = format!("simple string"); | ||
| let _s2 = format!("hello {}", "world"); | ||
|
|
||
| // Should not trigger (not format!) | ||
| let _s3 = "simple string".to_string(); | ||
| println!("hello {}", "world"); | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To match the other examples' configs.