Compare commits
2 Commits
master
...
rule/add-R
Author | SHA1 | Date | |
---|---|---|---|
![]() |
1b014dc06b | ||
![]() |
8415f8deec |
2
rules/S7453/metadata.json
Normal file
2
rules/S7453/metadata.json
Normal file
@ -0,0 +1,2 @@
|
||||
{
|
||||
}
|
24
rules/S7453/rust/metadata.json
Normal file
24
rules/S7453/rust/metadata.json
Normal 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"
|
||||
}
|
||||
}
|
32
rules/S7453/rust/rule.adoc
Normal file
32
rules/S7453/rust/rule.adoc
Normal 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
|
Loading…
x
Reference in New Issue
Block a user