Skip to content

Commit

Permalink
Merge pull request #178 from baoyachi/issue/151
Browse files Browse the repository at this point in the history
deny all cargo clippy check
  • Loading branch information
baoyachi authored Aug 22, 2024
2 parents 63816d6 + a8c9a2a commit 9d3df25
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 45 deletions.
7 changes: 7 additions & 0 deletions example_shadow/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,10 @@ shadow-rs = { path = "../" }

[build-dependencies]
shadow-rs = { path = "../" }

# Test cargo-clippy. For more detailed information about the issue, visit: https://github.com/baoyachi/shadow-rs/issues/151
[lints.clippy]
non_ascii_literal = "deny"
print_stdout = "deny"
needless_raw_strings = "deny"
needless_raw_string_hashes = "deny"
2 changes: 2 additions & 0 deletions example_shadow/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use shadow_rs::{shadow, Format};

shadow!(build);

#[allow(clippy::all, clippy::pedantic, clippy::restriction, clippy::nursery)]
fn main() {
let local_time = shadow_rs::DateTime::now().human_format();
println!("{local_time}");
Expand All @@ -25,6 +26,7 @@ fn main() {
}

#[allow(dead_code)]
#[allow(clippy::all, clippy::pedantic, clippy::restriction, clippy::nursery)]
pub fn print_build() {
println!("version:{}", build::VERSION);
println!("version:{}", build::CLAP_LONG_VERSION);
Expand Down
87 changes: 45 additions & 42 deletions src/gen_const.rs
Original file line number Diff line number Diff line change
@@ -1,84 +1,87 @@
macro_rules! gen_const {
($fn_name:ident, $fn_body:expr) => {
pub fn $fn_name() -> String {
$fn_body.to_string()
let (doc, content) = $fn_body;
format!(
"{}\n{}\n{}\n{}\n",
doc,
"#[allow(dead_code,missing_docs)]",
$crate::CARGO_CLIPPY_ALLOW_ALL,
content
)
}
};
}

const VERSION_BRANCH_CONST: &str = r##"
/// A long version string describing the project.
const VERSION_BRANCH_CONST: (&str, &str) = (
r#"/// A long version string describing the project.
/// The version string contains the package version, branch, commit hash, build time, and build environment on separate lines.
/// This constant is suitable for printing to the user.
#[allow(dead_code)]
#[allow(clippy::all,clippy::pedantic)]
pub const VERSION:&str = shadow_rs::formatcp!(r#"
/// This constant is suitable for printing to the user."#,
r##"pub const VERSION:&str = shadow_rs::formatcp!(r#"
pkg_version:{}
branch:{}
commit_hash:{}
build_time:{}
build_env:{},{}"#,PKG_VERSION, BRANCH, SHORT_COMMIT, BUILD_TIME, RUST_VERSION, RUST_CHANNEL
);"##;
);"##,
);

const VERSION_TAG_CONST: &str = r##"
/// A long version string describing the project.
const VERSION_TAG_CONST: (&str, &str) = (
r#"/// A long version string describing the project.
/// The version string contains the package version, current Git tag, commit hash, build time, and build environment on separate lines.
/// This constant is suitable for printing to the user.
#[allow(dead_code)]
#[allow(clippy::all,clippy::pedantic)]
pub const VERSION:&str = shadow_rs::formatcp!(r#"
/// This constant is suitable for printing to the user."#,
r##"pub const VERSION:&str = shadow_rs::formatcp!(r#"
pkg_version:{}
tag:{}
commit_hash:{}
build_time:{}
build_env:{},{}"#,PKG_VERSION, TAG, SHORT_COMMIT, BUILD_TIME, RUST_VERSION, RUST_CHANNEL
);"##;
);"##,
);

const CLAP_VERSION_BRANCH_CONST: &str = r##"#[allow(dead_code,missing_docs)]
#[allow(clippy::all,clippy::pedantic)]
#[deprecated = "Replaced with `CLAP_LONG_VERSION`"]
pub const CLAP_VERSION:&str = shadow_rs::formatcp!(r#"{}
const CLAP_VERSION_BRANCH_CONST: (&str, &str) = (
r#"#[deprecated = "Replaced with `CLAP_LONG_VERSION`"]"#,
r##"pub const CLAP_VERSION:&str = shadow_rs::formatcp!(r#"{}
branch:{}
commit_hash:{}
build_time:{}
build_env:{},{}"#,PKG_VERSION, BRANCH, SHORT_COMMIT, BUILD_TIME, RUST_VERSION, RUST_CHANNEL
);"##;
);"##,
);

const CLAP_VERSION_TAG_CONST: &str = r##"#[allow(dead_code,missing_docs)]
#[allow(clippy::all,clippy::pedantic)]
#[deprecated = "Replaced with `CLAP_LONG_VERSION`"]
pub const CLAP_VERSION:&str = shadow_rs::formatcp!(r#"{}
const CLAP_VERSION_TAG_CONST: (&str, &str) = (
r#"#[deprecated = "Replaced with `CLAP_LONG_VERSION`"]"#,
r##"pub const CLAP_VERSION:&str = shadow_rs::formatcp!(r#"{}
tag:{}
commit_hash:{}
build_time:{}
build_env:{},{}"#,PKG_VERSION, TAG, SHORT_COMMIT, BUILD_TIME, RUST_VERSION, RUST_CHANNEL
);"##;
);"##,
);

const CLAP_LONG_VERSION_BRANCH_CONST: &str = r##"
/// A long version string describing the project.
const CLAP_LONG_VERSION_BRANCH_CONST: (&str, &str) = (
r#"/// A long version string describing the project.
/// The version string contains the package version, branch, commit hash, build time, and build environment on separate lines.
/// This constant is intended to be used by clap or other CLI tools as a long version string.
#[allow(dead_code)]
#[allow(clippy::all,clippy::pedantic)]
pub const CLAP_LONG_VERSION:&str = shadow_rs::formatcp!(r#"{}
/// This constant is intended to be used by clap or other CLI tools as a long version string."#,
r##"pub const CLAP_LONG_VERSION:&str = shadow_rs::formatcp!(r#"{}
branch:{}
commit_hash:{}
build_time:{}
build_env:{},{}"#,PKG_VERSION, BRANCH, SHORT_COMMIT, BUILD_TIME, RUST_VERSION, RUST_CHANNEL
);"##;
);"##,
);

const CLAP_LONG_VERSION_TAG_CONST: &str = r##"
/// A long version string describing the project.
const CLAP_LONG_VERSION_TAG_CONST: (&str, &str) = (
r#"/// A long version string describing the project.
/// The version string contains the package version, current Git tag, commit hash, build time, and build environment on separate lines.
/// This constant is intended to be used by clap or other CLI tools as a long version string.
#[allow(dead_code)]
#[allow(clippy::all,clippy::pedantic)]
pub const CLAP_LONG_VERSION:&str = shadow_rs::formatcp!(r#"{}
/// This constant is intended to be used by clap or other CLI tools as a long version string."#,
r##"pub const CLAP_LONG_VERSION:&str = shadow_rs::formatcp!(r#"{}
tag:{}
commit_hash:{}
build_time:{}
build_env:{},{}"#,PKG_VERSION, TAG, SHORT_COMMIT, BUILD_TIME, RUST_VERSION, RUST_CHANNEL
);"##;
);"##,
);

gen_const!(version_branch_const, VERSION_BRANCH_CONST);
gen_const!(version_tag_const, VERSION_TAG_CONST);
Expand All @@ -99,8 +102,8 @@ mod tests {

#[test]
fn test_version_fn() {
assert!(version_tag_const().contains(VERSION_TAG_CONST));
assert!(clap_version_branch_const().contains(CLAP_VERSION_BRANCH_CONST));
assert!(clap_long_version_branch_const().contains(CLAP_LONG_VERSION_BRANCH_CONST));
assert!(version_tag_const().contains(VERSION_TAG_CONST.0));
assert!(clap_version_branch_const().contains(CLAP_VERSION_BRANCH_CONST.1));
assert!(clap_long_version_branch_const().contains(CLAP_LONG_VERSION_BRANCH_CONST.1));
}
}
1 change: 0 additions & 1 deletion src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use std::process::{Command, Stdio};

const BRANCH_DOC: &str = r#"
The name of the Git branch that this project was built from.
This constant will be empty if the branch cannot be determined."#;
pub const BRANCH: ShadowConst = "BRANCH";
const TAG_DOC: &str = r#"
Expand Down
12 changes: 10 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ pub trait Format {

const SHADOW_RS: &str = "shadow.rs";

const CARGO_CLIPPY_ALLOW_ALL: &str =
"#[allow(clippy::all, clippy::pedantic, clippy::restriction, clippy::nursery)]";

/// Add a module with the provided name which contains the build information generated by `shadow-rs`.
///
/// # Example
Expand Down Expand Up @@ -449,26 +452,30 @@ impl Shadow {

fn write_const(&mut self, shadow_const: ShadowConst, val: ConstVal) -> SdResult<()> {
let desc = format!("#[doc=r#\"{}\"#]", val.desc);

let define = match val.t {
ConstType::Str => format!(
"#[allow(dead_code)]\n\
#[allow(clippy::all,clippy::pedantic)]\n\
{}\n\
pub const {} :{} = r#\"{}\"#;",
CARGO_CLIPPY_ALLOW_ALL,
shadow_const.to_ascii_uppercase(),
ConstType::Str,
val.v
),
ConstType::Bool => format!(
"#[allow(dead_code)]\n\
{}\n\
pub const {} :{} = {};",
CARGO_CLIPPY_ALLOW_ALL,
shadow_const.to_ascii_uppercase(),
ConstType::Bool,
val.v.parse::<bool>().unwrap()
),
ConstType::Slice => format!(
"#[allow(dead_code)]\n\
{}\n\
pub const {} :{} = &{:?};",
CARGO_CLIPPY_ALLOW_ALL,
shadow_const.to_ascii_uppercase(),
ConstType::Slice,
val.v.as_bytes()
Expand Down Expand Up @@ -535,6 +542,7 @@ impl Shadow {
let everything_define = format!(
"/// Prints all built-in `shadow-rs` build constants to standard output.\n\
#[allow(dead_code)]\n\
{CARGO_CLIPPY_ALLOW_ALL}\n\
pub fn print_build_in() {\
{{print_val}}\
}\n",
Expand Down

0 comments on commit 9d3df25

Please sign in to comment.