Skip to content

Commit a371558

Browse files
authored
Merge pull request #1093 from oli-obk/serde_specific_lint
lint on implementing `visit_string` without also implementing `visit_str`
2 parents 94fcdad + b4ee911 commit a371558

File tree

11 files changed

+163
-7
lines changed

11 files changed

+163
-7
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: 4 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,8 @@ 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);
172+
reg.register_early_lint_pass(box utils::internal_lints::Clippy);
170173
reg.register_late_lint_pass(box types::TypePass);
171174
reg.register_late_lint_pass(box booleans::NonminimalBool);
172175
reg.register_late_lint_pass(box misc::TopLevelRefPass);
@@ -399,6 +402,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
399402
regex::TRIVIAL_REGEX,
400403
returns::LET_AND_RETURN,
401404
returns::NEEDLESS_RETURN,
405+
serde::SERDE_API_MISUSE,
402406
strings::STRING_LIT_AS_BYTES,
403407
swap::ALMOST_SWAPPED,
404408
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, paths};
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, &paths::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+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
use rustc::lint::*;
2+
use utils::span_lint;
3+
use syntax::parse::token::InternedString;
4+
use syntax::ast::*;
5+
6+
/// **What it does:** This lint checks for various things we like to keep tidy in clippy
7+
///
8+
/// **Why is this bad?** ???
9+
///
10+
/// **Known problems:** None.
11+
///
12+
/// **Example:** wrong ordering of the util::paths constants
13+
declare_lint! {
14+
pub CLIPPY_LINTS_INTERNAL, Allow,
15+
"Various things that will negatively affect your clippy experience"
16+
}
17+
18+
19+
#[derive(Copy, Clone)]
20+
pub struct Clippy;
21+
22+
impl LintPass for Clippy {
23+
fn get_lints(&self) -> LintArray {
24+
lint_array!(CLIPPY_LINTS_INTERNAL)
25+
}
26+
}
27+
28+
impl EarlyLintPass for Clippy {
29+
fn check_crate(&mut self, cx: &EarlyContext, krate: &Crate) {
30+
if let Some(utils) = krate.module.items.iter().find(|item| item.ident.name.as_str() == "utils") {
31+
if let ItemKind::Mod(ref utils_mod) = utils.node {
32+
if let Some(paths) = utils_mod.items.iter().find(|item| item.ident.name.as_str() == "paths") {
33+
if let ItemKind::Mod(ref paths_mod) = paths.node {
34+
let mut last_name: Option<InternedString> = None;
35+
for item in &paths_mod.items {
36+
let name = item.ident.name.as_str();
37+
if let Some(ref last_name) = last_name {
38+
if **last_name > *name {
39+
span_lint(cx,
40+
CLIPPY_LINTS_INTERNAL,
41+
item.span,
42+
"this constant should be before the previous constant due to lexical ordering",
43+
);
44+
}
45+
}
46+
last_name = Some(name);
47+
}
48+
}
49+
}
50+
}
51+
}
52+
}
53+
}

clippy_lints/src/utils/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub mod conf;
2525
mod hir;
2626
pub mod paths;
2727
pub mod sugg;
28+
pub mod internal_lints;
2829
pub use self::hir::{SpanlessEq, SpanlessHash};
2930

3031
pub type MethodArgs = HirVec<P<Expr>>;

clippy_lints/src/utils/paths.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ pub const REGEX_BYTES_SET_NEW: [&'static str; 5] = ["regex", "re_set", "bytes",
5454
pub const REGEX_NEW: [&'static str; 4] = ["regex", "re_unicode", "Regex", "new"];
5555
pub const REGEX_SET_NEW: [&'static str; 5] = ["regex", "re_set", "unicode", "RegexSet", "new"];
5656
pub const RESULT: [&'static str; 3] = ["core", "result", "Result"];
57+
pub const SERDE_DE_VISITOR: [&'static str; 3] = ["serde", "de", "Visitor"];
5758
pub const STRING: [&'static str; 3] = ["collections", "string", "String"];
5859
pub const TRANSMUTE: [&'static str; 4] = ["core", "intrinsics", "", "transmute"];
5960
pub const VEC: [&'static str; 3] = ["collections", "vec", "Vec"];

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+
}

tests/dogfood.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ fn dogfood() {
1717
let mut s = String::new();
1818
s.push_str(" -L target/debug/");
1919
s.push_str(" -L target/debug/deps");
20-
s.push_str(" -Zextra-plugins=clippy -Ltarget_recur/debug -Dclippy_pedantic -Dclippy");
20+
s.push_str(" -Zextra-plugins=clippy -Ltarget_recur/debug -Dclippy_pedantic -Dclippy -Dclippy_lints_internal");
2121
config.target_rustcflags = Some(s);
2222
if let Ok(name) = var("TESTNAME") {
2323
config.filter = Some(name.to_owned())
@@ -29,6 +29,7 @@ fn dogfood() {
2929
}
3030

3131
config.mode = cfg_mode;
32+
config.verbose = true;
3233

3334
let files = ["src/main.rs", "src/lib.rs", "clippy_lints/src/lib.rs"];
3435

util/update_lints.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,10 @@ def main(print_only=False, check=False):
150150
return
151151

152152
# collect all lints from source files
153-
for root, _, files in os.walk('clippy_lints/src'):
154-
for fn in files:
155-
if fn.endswith('.rs'):
156-
collect(lints, deprecated_lints, restriction_lints,
157-
os.path.join(root, fn))
153+
for fn in os.listdir('clippy_lints/src'):
154+
if fn.endswith('.rs'):
155+
collect(lints, deprecated_lints, restriction_lints,
156+
os.path.join('clippy_lints', 'src', fn))
158157

159158
# determine version
160159
with open('Cargo.toml') as fp:

0 commit comments

Comments
 (0)