From ef35f53a806cd909d5410aa63927cebf63acf193 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:46:46 +0000 Subject: [PATCH] Create rule S4325: Redundant casts should be avoided (#4729) * Add rust to rule S4325 * Update RSPEC --------- Co-authored-by: yassin-kammoun-sonarsource Co-authored-by: yassin-kammoun-sonarsource --- rules/S4325/javascript/metadata.json | 2 +- rules/S4325/metadata.json | 33 ++++++++++++++++++++++++++++ rules/S4325/rust/metadata.json | 7 ++++++ rules/S4325/rust/rule.adoc | 28 +++++++++++++++++++++++ 4 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 rules/S4325/rust/metadata.json create mode 100644 rules/S4325/rust/rule.adoc diff --git a/rules/S4325/javascript/metadata.json b/rules/S4325/javascript/metadata.json index 267a466d02..99458d9a53 100644 --- a/rules/S4325/javascript/metadata.json +++ b/rules/S4325/javascript/metadata.json @@ -31,4 +31,4 @@ "compatibleLanguages": [ "ts" ] -} \ No newline at end of file +} diff --git a/rules/S4325/metadata.json b/rules/S4325/metadata.json index 2c63c08510..2170c7558c 100644 --- a/rules/S4325/metadata.json +++ b/rules/S4325/metadata.json @@ -1,2 +1,35 @@ { + "title": "Redundant casts and non-null assertions should be avoided", + "type": "CODE_SMELL", + "code": { + "impacts": { + "MAINTAINABILITY": "LOW" + }, + "attribute": "CLEAR" + }, + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "1min" + }, + "tags": [ + "redundant", + "type-dependent" + ], + "extra": { + "replacementRules": [ + + ], + "legacyKeys": [ + + ] + }, + "defaultSeverity": "Minor", + "ruleSpecification": "RSPEC-4325", + "sqKey": "S4325", + "scope": "Main", + "defaultQualityProfiles": [ + "Sonar way" + ], + "quickfix": "covered" } diff --git a/rules/S4325/rust/metadata.json b/rules/S4325/rust/metadata.json new file mode 100644 index 0000000000..3dcd36c855 --- /dev/null +++ b/rules/S4325/rust/metadata.json @@ -0,0 +1,7 @@ +{ + "title": "Redundant casts should be avoided", + "tags": [ + "redundant", + "clippy" + ] +} diff --git a/rules/S4325/rust/rule.adoc b/rules/S4325/rust/rule.adoc new file mode 100644 index 0000000000..ccef2ce096 --- /dev/null +++ b/rules/S4325/rust/rule.adoc @@ -0,0 +1,28 @@ +== Why is this an issue? + +In Rust, type casting is a mechanism used to convert a value from one type to another. This is often necessary when working with different types that are compatible but not the same. However, unnecessary casts can occur when the type conversion is redundant because the compiler already knows the type based on the context or explicit type declarations. + +Unnecessary casts should be avoided in Rust code as they add unnecessary complexity, clutter the code, and can lead to confusion. They also make the code less idiomatic and harder to read. + +[source,rust,diff-id=1,diff-type=noncompliant] +---- +fn get_length(value: &str) -> usize { + let length = value.len() as usize; // Noncompliant: 'value.len()' already returns a 'usize' + length +} +---- + +Remove all unnecessary casts based on the contextual typing information, as inferred by the Rust compiler. + +[source,rust,diff-id=1,diff-type=compliant] +---- +fn get_length(value: &str) -> usize { + let length = value.len(); + length +} +---- + +== Resources +=== Documentation + +* Clippy Lints - https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_cast