![github-actions[bot]](/assets/img/avatar_default.png)
* Add rust to rule S4325 * Update RSPEC --------- Co-authored-by: yassin-kammoun-sonarsource <yassin-kammoun-sonarsource@users.noreply.github.com> Co-authored-by: yassin-kammoun-sonarsource <yassin.kammoun@sonarsource.com>
29 lines
1.1 KiB
Plaintext
29 lines
1.1 KiB
Plaintext
== 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
|