Compare commits

...

2 Commits

Author SHA1 Message Date
yassin-kammoun-sonarsource
1b014dc06b Update RSPEC 2025-03-26 10:56:28 +01:00
yassin-kammoun-sonarsource
8415f8deec Create rule S7453 2025-03-25 14:37:32 +00:00
3 changed files with 58 additions and 0 deletions

View File

@ -0,0 +1,2 @@
{
}

View File

@ -0,0 +1,24 @@
{
"title": "Functions should not return mutable references from immutable parameters",
"type": "BUG",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [
"clippy"
],
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-7453",
"sqKey": "S7453",
"scope": "All",
"defaultQualityProfiles": ["Sonar way"],
"quickfix": "unknown",
"code": {
"impacts": {
"RELIABILITY": "MEDIUM"
},
"attribute": "LOGICAL"
}
}

View File

@ -0,0 +1,32 @@
== Why is this an issue?
Creating a mutable reference from an immutable one is unsound because it can lead to multiple live mutable references to the same object, breaking Rust's guarantees of memory safety. Such patterns are particularly dangerous if unsafe code is present as it can lead to undefined behavior.
=== Code examples
==== Noncompliant code example
[source,rust,diff-id=1,diff-type=noncompliant]
----
fn foo(x: &Foo) -> &mut Bar {
unsafe {
// Noncompliant: Converting immutable reference to mutable.
&mut *(x as *const Foo as *mut Foo).bar
}
}
----
==== Compliant solution
[source,rust,diff-id=1,diff-type=compliant]
----
fn foo(x: &mut Foo) -> &mut Bar {
// Compliant: Taking a mutable reference and returning a mutable reference.
&mut x.bar
}
----
== Resources
=== Documentation
* Clippy Lints - https://rust-lang.github.io/rust-clippy/master/index.html#mut_from_ref