Skip to content

Commit 319c66a

Browse files
committed
lint on implementing visit_string without also implementing visit_str
1 parent b14114f commit 319c66a

File tree

6 files changed

+101
-1
lines changed

6 files changed

+101
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ All notable changes to this project will be documented in this file.
246246
[`result_unwrap_used`]: https://github.com/Manishearth/rust-clippy/wiki#result_unwrap_used
247247
[`reverse_range_loop`]: https://github.com/Manishearth/rust-clippy/wiki#reverse_range_loop
248248
[`search_is_some`]: https://github.com/Manishearth/rust-clippy/wiki#search_is_some
249+
[`serde_api_misuse`]: https://github.com/Manishearth/rust-clippy/wiki#serde_api_misuse
249250
[`shadow_reuse`]: https://github.com/Manishearth/rust-clippy/wiki#shadow_reuse
250251
[`shadow_same`]: https://github.com/Manishearth/rust-clippy/wiki#shadow_same
251252
[`shadow_unrelated`]: https://github.com/Manishearth/rust-clippy/wiki#shadow_unrelated

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ lazy_static = "0.1.15"
3434
regex = "0.1.71"
3535
rustc-serialize = "0.3"
3636
clippy-mini-macro-test = { version = "0.1", path = "mini-macro" }
37+
serde = "0.7"
3738

3839

3940
[features]

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Table of contents:
1717

1818
## Lints
1919

20-
There are 158 lints included in this crate:
20+
There are 159 lints included in this crate:
2121

2222
name | default | meaning
2323
---------------------------------------------------------------------------------------------------------------------|---------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
@@ -136,6 +136,7 @@ name
136136
[result_unwrap_used](https://github.com/Manishearth/rust-clippy/wiki#result_unwrap_used) | allow | using `Result.unwrap()`, which might be better handled
137137
[reverse_range_loop](https://github.com/Manishearth/rust-clippy/wiki#reverse_range_loop) | warn | Iterating over an empty range, such as `10..0` or `5..5`
138138
[search_is_some](https://github.com/Manishearth/rust-clippy/wiki#search_is_some) | warn | using an iterator search followed by `is_some()`, which is more succinctly expressed as a call to `any()`
139+
[serde_api_misuse](https://github.com/Manishearth/rust-clippy/wiki#serde_api_misuse) | warn | Various things that will negatively affect your serde experience
139140
[shadow_reuse](https://github.com/Manishearth/rust-clippy/wiki#shadow_reuse) | allow | rebinding a name to an expression that re-uses the original value, e.g. `let x = x + 1`
140141
[shadow_same](https://github.com/Manishearth/rust-clippy/wiki#shadow_same) | allow | rebinding a name to itself, e.g. `let mut x = &mut x`
141142
[shadow_unrelated](https://github.com/Manishearth/rust-clippy/wiki#shadow_unrelated) | allow | The name is re-bound without even using the original value

clippy_lints/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ pub mod ptr_arg;
114114
pub mod ranges;
115115
pub mod regex;
116116
pub mod returns;
117+
pub mod serde;
117118
pub mod shadow;
118119
pub mod strings;
119120
pub mod swap;
@@ -167,6 +168,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
167168
store.register_removed("string_to_string", "using `string::to_string` is common even today and specialization will likely happen soon");
168169
// end deprecated lints, do not remove this comment, it’s used in `update_lints`
169170

171+
reg.register_late_lint_pass(box serde::Serde);
170172
reg.register_late_lint_pass(box types::TypePass);
171173
reg.register_late_lint_pass(box booleans::NonminimalBool);
172174
reg.register_late_lint_pass(box misc::TopLevelRefPass);
@@ -399,6 +401,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
399401
regex::TRIVIAL_REGEX,
400402
returns::LET_AND_RETURN,
401403
returns::NEEDLESS_RETURN,
404+
serde::SERDE_API_MISUSE,
402405
strings::STRING_LIT_AS_BYTES,
403406
swap::ALMOST_SWAPPED,
404407
swap::MANUAL_SWAP,

clippy_lints/src/serde.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
use rustc::lint::*;
2+
use rustc::hir::*;
3+
use utils::{span_lint, get_trait_def_id};
4+
5+
/// **What it does:** This lint checks for mis-uses of the serde API
6+
///
7+
/// **Why is this bad?** Serde is very finnicky about how its API should be used, but the type system can't be used to enforce it (yet)
8+
///
9+
/// **Known problems:** None.
10+
///
11+
/// **Example:** implementing `Visitor::visit_string` but not `Visitor::visit_str`
12+
declare_lint! {
13+
pub SERDE_API_MISUSE, Warn,
14+
"Various things that will negatively affect your serde experience"
15+
}
16+
17+
18+
#[derive(Copy, Clone)]
19+
pub struct Serde;
20+
21+
impl LintPass for Serde {
22+
fn get_lints(&self) -> LintArray {
23+
lint_array!(SERDE_API_MISUSE)
24+
}
25+
}
26+
27+
impl LateLintPass for Serde {
28+
fn check_item(&mut self, cx: &LateContext, item: &Item) {
29+
if let ItemImpl(_, _, _, Some(ref trait_ref), _, ref items) = item.node {
30+
let did = cx.tcx.expect_def(trait_ref.ref_id).def_id();
31+
if let Some(visit_did) = get_trait_def_id(cx, &["serde", "de", "Visitor"]) {
32+
if did == visit_did {
33+
let mut seen_str = None;
34+
let mut seen_string = None;
35+
for item in items {
36+
match &*item.name.as_str() {
37+
"visit_str" => seen_str = Some(item.span),
38+
"visit_string" => seen_string = Some(item.span),
39+
_ => {},
40+
}
41+
}
42+
if let Some(span) = seen_string {
43+
if seen_str.is_none() {
44+
span_lint(cx,
45+
SERDE_API_MISUSE,
46+
span,
47+
"you should not implement `visit_string` without also implementing `visit_str`",
48+
);
49+
}
50+
}
51+
}
52+
}
53+
}
54+
}
55+
}

tests/compile-fail/serde.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#![feature(plugin)]
2+
#![plugin(clippy)]
3+
#![deny(serde_api_misuse)]
4+
#![allow(dead_code)]
5+
6+
extern crate serde;
7+
8+
struct A;
9+
10+
impl serde::de::Visitor for A {
11+
type Value = ();
12+
fn visit_str<E>(&mut self, _v: &str) -> Result<Self::Value, E>
13+
where E: serde::Error,
14+
{
15+
unimplemented!()
16+
}
17+
18+
fn visit_string<E>(&mut self, _v: String) -> Result<Self::Value, E>
19+
where E: serde::Error,
20+
{
21+
unimplemented!()
22+
}
23+
}
24+
25+
struct B;
26+
27+
impl serde::de::Visitor for B {
28+
type Value = ();
29+
30+
fn visit_string<E>(&mut self, _v: String) -> Result<Self::Value, E>
31+
//~^ ERROR you should not implement `visit_string` without also implementing `visit_str`
32+
where E: serde::Error,
33+
{
34+
unimplemented!()
35+
}
36+
}
37+
38+
fn main() {
39+
}

0 commit comments

Comments
 (0)