diff --git a/rules/S7445/metadata.json b/rules/S7445/metadata.json new file mode 100644 index 0000000000..2c63c08510 --- /dev/null +++ b/rules/S7445/metadata.json @@ -0,0 +1,2 @@ +{ +} diff --git a/rules/S7445/rust/metadata.json b/rules/S7445/rust/metadata.json new file mode 100644 index 0000000000..9254845755 --- /dev/null +++ b/rules/S7445/rust/metadata.json @@ -0,0 +1,24 @@ +{ + "title": "`env!` should be preferred over `option_env!`", + "type": "BUG", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "5min" + }, + "tags": [ + "clippy" + ], + "defaultSeverity": "Major", + "ruleSpecification": "RSPEC-7445", + "sqKey": "S7445", + "scope": "All", + "defaultQualityProfiles": ["Sonar way"], + "quickfix": "unknown", + "code": { + "impacts": { + "RELIABILITY": "LOW" + }, + "attribute": "LOGICAL" + } +} diff --git a/rules/S7445/rust/rule.adoc b/rules/S7445/rust/rule.adoc new file mode 100644 index 0000000000..3365ba1839 --- /dev/null +++ b/rules/S7445/rust/rule.adoc @@ -0,0 +1,24 @@ +== Why is this an issue? + +Unwrapping the result of `option_env!` will panic at runtime if the environment variable doesn't exist, whereas `env!` catches it at compile-time, ensuring safer code execution. + +=== Code examples + +==== Noncompliant code example + +[source,rust,diff-id=1,diff-type=noncompliant] +---_ +let _ = option_env!("HOME").unwrap(); // Noncompliant: Can panic at runtime. +---_ + +==== Compliant solution + +[source,rust,diff-id=1,diff-type=compliant] +---_ +let _ = env!("HOME"); // Compliant: Ensures compile-time checking. +---_ + +== Resources +=== Documentation + +* Clippy Lints - https://rust-lang.github.io/rust-clippy/master/index.html#option_env_unwrap