Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 118 additions & 26 deletions examples/supplementary/Cargo.lock

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"
Comment on lines +1 to +6
Copy link
Collaborator

@smoelius smoelius May 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
[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"
[build]
target-dir = "../../target/examples"
[target.'cfg(all())']
linker = "dylint-link"

To match the other examples' configs.

1 change: 1 addition & 0 deletions examples/supplementary/format_concat_args/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file can be removed. A .gitignore in a parent directory accomplishes this.

26 changes: 26 additions & 0 deletions examples/supplementary/format_concat_args/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[package]
name = "format_concat_args"
version = "0.1.0"
Copy link
Collaborator

@smoelius smoelius May 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
version = "0.1.0"
version = "4.1.0"

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"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
edition = "2021"
edition = "2024"

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
12 changes: 12 additions & 0 deletions examples/supplementary/format_concat_args/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# format_concat_args


## Examples


## Building and Testing

## Limitations

## References

3 changes: 3 additions & 0 deletions examples/supplementary/format_concat_args/rust-toolchain
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[toolchain]
channel = "nightly-2025-02-20"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
channel = "nightly-2025-02-20"
channel = "nightly-2025-04-03"

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 clippy_utils you are currently using.

components = ["llvm-tools-preview", "rustc-dev"]
69 changes: 69 additions & 0 deletions examples/supplementary/format_concat_args/src/lib.rs
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"), &[]);
}
10 changes: 10 additions & 0 deletions examples/supplementary/format_concat_args/test_lint.rs
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");
}
Loading
Loading