Create rule S7451: Remainder operations with 1
or -1
should be avoided (#4797)
* Create rule S7451 * 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>
This commit is contained in:
parent
40b6cf5d12
commit
28e7cab961
2
rules/S7451/metadata.json
Normal file
2
rules/S7451/metadata.json
Normal file
@ -0,0 +1,2 @@
|
||||
{
|
||||
}
|
24
rules/S7451/rust/metadata.json
Normal file
24
rules/S7451/rust/metadata.json
Normal file
@ -0,0 +1,24 @@
|
||||
{
|
||||
"title": "Remainder operations with `1` or `-1` should be avoided",
|
||||
"type": "BUG",
|
||||
"status": "ready",
|
||||
"remediation": {
|
||||
"func": "Constant\/Issue",
|
||||
"constantCost": "5min"
|
||||
},
|
||||
"tags": [
|
||||
"clippy"
|
||||
],
|
||||
"defaultSeverity": "Major",
|
||||
"ruleSpecification": "RSPEC-7451",
|
||||
"sqKey": "S7451",
|
||||
"scope": "All",
|
||||
"defaultQualityProfiles": ["Sonar way"],
|
||||
"quickfix": "unknown",
|
||||
"code": {
|
||||
"impacts": {
|
||||
"RELIABILITY": "HIGH"
|
||||
},
|
||||
"attribute": "LOGICAL"
|
||||
}
|
||||
}
|
28
rules/S7451/rust/rule.adoc
Normal file
28
rules/S7451/rust/rule.adoc
Normal file
@ -0,0 +1,28 @@
|
||||
== Why is this an issue?
|
||||
|
||||
Getting the remainder of an integer division by one always results in zero, making the operation redundant. Using minus one as the divisor can cause panic or overflow issues.
|
||||
|
||||
=== Code examples
|
||||
|
||||
==== Noncompliant code example
|
||||
|
||||
[source,rust,diff-id=1,diff-type=noncompliant]
|
||||
----
|
||||
let x = 1;
|
||||
let a = x % 1; // Noncompliant: Remainder of division by one.
|
||||
let b = x % -1; // Noncompliant: Remainder of division by minus one.
|
||||
----
|
||||
|
||||
==== Compliant solution
|
||||
|
||||
[source,rust,diff-id=1,diff-type=compliant]
|
||||
----
|
||||
let x = 1;
|
||||
let a = 0; // Compliant: Directly assigning zero instead of using `% 1`.
|
||||
let b = 0; // Compliant: Directly assigning zero instead of using `% -1`.
|
||||
----
|
||||
|
||||
== Resources
|
||||
=== Documentation
|
||||
|
||||
* Clippy Lints - https://rust-lang.github.io/rust-clippy/master/index.html#modulo_one
|
Loading…
x
Reference in New Issue
Block a user