From 7b234485eb0948c04c4cd71ce3756c7ca80fd22d 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:09:52 +0000 Subject: [PATCH] Create rule S7428 Case mismatches in pattern arms of match expressions should be avoided (#4769) * Create rule S7428 * Update rule.adoc * Update metadata.json * Update metadata.json * Update metadata.json --------- Co-authored-by: sallaigy Co-authored-by: Gyula Sallai --- rules/S7428/metadata.json | 2 ++ rules/S7428/rust/metadata.json | 24 ++++++++++++++++++++++++ rules/S7428/rust/rule.adoc | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 rules/S7428/metadata.json create mode 100644 rules/S7428/rust/metadata.json create mode 100644 rules/S7428/rust/rule.adoc diff --git a/rules/S7428/metadata.json b/rules/S7428/metadata.json new file mode 100644 index 0000000000..2c63c08510 --- /dev/null +++ b/rules/S7428/metadata.json @@ -0,0 +1,2 @@ +{ +} diff --git a/rules/S7428/rust/metadata.json b/rules/S7428/rust/metadata.json new file mode 100644 index 0000000000..e18703bc41 --- /dev/null +++ b/rules/S7428/rust/metadata.json @@ -0,0 +1,24 @@ +{ + "title": "Case mismatches in pattern arms of match expressions should be avoided", + "type": "BUG", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "5min" + }, + "tags": [ + "clippy" + ], + "defaultSeverity": "Blocker", + "ruleSpecification": "RSPEC-7428", + "sqKey": "S7428", + "scope": "All", + "defaultQualityProfiles": ["Sonar way"], + "quickfix": "unknown", + "code": { + "impacts": { + "RELIABILITY": "HIGH" + }, + "attribute": "LOGICAL" + } +} diff --git a/rules/S7428/rust/rule.adoc b/rules/S7428/rust/rule.adoc new file mode 100644 index 0000000000..4c2da502f8 --- /dev/null +++ b/rules/S7428/rust/rule.adoc @@ -0,0 +1,32 @@ + +== Why is this an issue? +Case mismatches in pattern arms make some arms unreachable, likely leading to logic errors that can be difficult to debug. + + +=== Code examples + +==== Noncompliant code example +[source,rust,diff-id=1,diff-type=noncompliant] +---- +match &*text.to_ascii_lowercase() { + "foo" => {}, + "Bar" => {}, // Noncompliant: This arm is unreachable. + _ => {}, +} +---- + +==== Compliant solution + +[source,rust,diff-id=1,diff-type=compliant] +---- +match &*text.to_ascii_lowercase() { + "foo" => {}, + "bar" => {}, + _ => {}, +} +---- + +== Resources +=== Documentation + +* Clippy Lints - https://rust-lang.github.io/rust-clippy/master/index.html#match_str_case_mismatch