From 0b46bc20f9f3a38db3481292672a0678f50fa89d Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed, 19 Mar 2025 14:06:12 +0100 Subject: [PATCH] Create rule S7447: File open options should be consistent (#4791) * Create rule S7447 * Update RSPEC --------- Co-authored-by: yassin-kammoun-sonarsource Co-authored-by: yassin-kammoun-sonarsource --- rules/S7447/metadata.json | 2 ++ rules/S7447/rust/metadata.json | 24 ++++++++++++++++++++++++ rules/S7447/rust/rule.adoc | 28 ++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+) create mode 100644 rules/S7447/metadata.json create mode 100644 rules/S7447/rust/metadata.json create mode 100644 rules/S7447/rust/rule.adoc diff --git a/rules/S7447/metadata.json b/rules/S7447/metadata.json new file mode 100644 index 0000000000..2c63c08510 --- /dev/null +++ b/rules/S7447/metadata.json @@ -0,0 +1,2 @@ +{ +} diff --git a/rules/S7447/rust/metadata.json b/rules/S7447/rust/metadata.json new file mode 100644 index 0000000000..08d303e5c2 --- /dev/null +++ b/rules/S7447/rust/metadata.json @@ -0,0 +1,24 @@ +{ + "title": "File open options should be consistent", + "type": "BUG", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "5min" + }, + "tags": [ + "clippy" + ], + "defaultSeverity": "Major", + "ruleSpecification": "RSPEC-7447", + "sqKey": "S7447", + "scope": "All", + "defaultQualityProfiles": ["Sonar way"], + "quickfix": "unknown", + "code": { + "impacts": { + "RELIABILITY": "HIGH" + }, + "attribute": "LOGICAL" + } +} diff --git a/rules/S7447/rust/rule.adoc b/rules/S7447/rust/rule.adoc new file mode 100644 index 0000000000..e1b2408735 --- /dev/null +++ b/rules/S7447/rust/rule.adoc @@ -0,0 +1,28 @@ +== Why is this an issue? + +Using contradictory or nonsensical combinations of file open options, such as `read(true)` with `truncate(true)`, can lead to code that is confusing and harder to read. In some cases, it can result in runtime errors or undefined behavior, potentially causing data corruption or loss. + +=== Code examples + +==== Noncompliant code example + +[source,rust,diff-id=1,diff-type=noncompliant] +---- +use std::fs::OpenOptions; + +OpenOptions::new().read(true).truncate(true); // Noncompliant: Invalid combination of file open options. +---- + +==== Compliant solution + +[source,rust,diff-id=1,diff-type=compliant] +---- +use std::fs::OpenOptions; + +OpenOptions::new().write(true).truncate(true); // Compliant: Valid combination of file open options. +---- + +== Resources +=== Documentation + +* Clippy Lints - https://rust-lang.github.io/rust-clippy/master/index.html#nonsensical_open_options