diff --git a/example_shadow/Cargo.toml b/example_shadow/Cargo.toml index d1ec02e..7434ac6 100644 --- a/example_shadow/Cargo.toml +++ b/example_shadow/Cargo.toml @@ -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" \ No newline at end of file diff --git a/example_shadow/src/main.rs b/example_shadow/src/main.rs index c5accf0..1cfb3f9 100644 --- a/example_shadow/src/main.rs +++ b/example_shadow/src/main.rs @@ -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}"); @@ -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); diff --git a/src/gen_const.rs b/src/gen_const.rs index 1388360..d6aed16 100644 --- a/src/gen_const.rs +++ b/src/gen_const.rs @@ -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); @@ -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)); } } diff --git a/src/git.rs b/src/git.rs index e627e43..ae00c87 100644 --- a/src/git.rs +++ b/src/git.rs @@ -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#" diff --git a/src/lib.rs b/src/lib.rs index a48e82f..8bb3017 100755 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 @@ -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::().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() @@ -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",