Compare commits
3 Commits
master
...
rule/add-R
Author | SHA1 | Date | |
---|---|---|---|
![]() |
82be31de71 | ||
![]() |
32914e08dd | ||
![]() |
0f59803695 |
2
rules/S7460/metadata.json
Normal file
2
rules/S7460/metadata.json
Normal file
@ -0,0 +1,2 @@
|
||||
{
|
||||
}
|
24
rules/S7460/rust/metadata.json
Normal file
24
rules/S7460/rust/metadata.json
Normal file
@ -0,0 +1,24 @@
|
||||
{
|
||||
"title": "Serde `visit_str` method should be implemented when `visit_string` is implemented",
|
||||
"type": "BUG",
|
||||
"status": "ready",
|
||||
"remediation": {
|
||||
"func": "Constant\/Issue",
|
||||
"constantCost": "5min"
|
||||
},
|
||||
"tags": [
|
||||
"clippy"
|
||||
],
|
||||
"defaultSeverity": "Major",
|
||||
"ruleSpecification": "RSPEC-7460",
|
||||
"sqKey": "S7460",
|
||||
"scope": "All",
|
||||
"defaultQualityProfiles": ["Sonar way"],
|
||||
"quickfix": "unknown",
|
||||
"code": {
|
||||
"impacts": {
|
||||
"RELIABILITY": "MEDIUM"
|
||||
},
|
||||
"attribute": "LOGICAL"
|
||||
}
|
||||
}
|
63
rules/S7460/rust/rule.adoc
Normal file
63
rules/S7460/rust/rule.adoc
Normal file
@ -0,0 +1,63 @@
|
||||
== Why is this an issue?
|
||||
|
||||
Serde is a popular framework in the Rust ecosystem for serializing and deserializing data. It provides a flexible and efficient way to convert Rust data structures into various formats (e.g., JSON, YAML) and vice versa.
|
||||
|
||||
One of the core components of Serde is the `Visitor` trait, which allows custom deserialization logic by visiting each element of the data structure. According to Serde's link:https://docs.rs/serde/latest/serde/de/trait.Visitor.html#method.visit_str[documentation], any implementation of the `Visitor` trait that implements the `visit_string` method must also implement the `visit_str` method. Failing to do so can lead to unexpected behavior.
|
||||
|
||||
This rule ensures that implementations of the `Visitor` trait adhere to this requirement, promoting correctness and preventing subtle bugs in deserialization logic.
|
||||
|
||||
=== Code examples
|
||||
|
||||
==== Noncompliant code example
|
||||
|
||||
[source,rust,diff-id=1,diff-type=noncompliant]
|
||||
----
|
||||
struct A;
|
||||
|
||||
impl<'de> serde::de::Visitor<'de> for A {
|
||||
type Value = ();
|
||||
|
||||
fn expecting(&self, _: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn visit_string<E>(self, _v: String) -> Result<Self::Value, E>
|
||||
where
|
||||
E: serde::de::Error,
|
||||
{
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
----
|
||||
|
||||
==== Compliant solution
|
||||
|
||||
[source,rust,diff-id=1,diff-type=compliant]
|
||||
----
|
||||
impl<'de> serde::de::Visitor<'de> for A {
|
||||
type Value = ();
|
||||
|
||||
fn expecting(&self, _: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn visit_str<E>(self, _v: &str) -> Result<Self::Value, E>
|
||||
where
|
||||
E: serde::de::Error,
|
||||
{
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn visit_string<E>(self, _v: String) -> Result<Self::Value, E>
|
||||
where
|
||||
E: serde::de::Error,
|
||||
{
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
----
|
||||
|
||||
== Resources
|
||||
=== Documentation
|
||||
|
||||
* Clippy Lints - https://rust-lang.github.io/rust-clippy/master/index.html#serde_api_misuse
|
Loading…
x
Reference in New Issue
Block a user