]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/serde_api.rs
Fix ICE for issues 2767, 2499, 1782
[rust.git] / clippy_lints / src / serde_api.rs
1 use rustc::lint::*;
2 use rustc::hir::*;
3 use utils::{get_trait_def_id, paths, span_lint};
4
5 /// **What it does:** Checks for mis-uses of the serde API.
6 ///
7 /// **Why is this bad?** Serde is very finnicky about how its API should be
8 /// used, but the type system can't be used to enforce it (yet?).
9 ///
10 /// **Known problems:** None.
11 ///
12 /// **Example:** Implementing `Visitor::visit_string` but not
13 /// `Visitor::visit_str`.
14 declare_clippy_lint! {
15     pub SERDE_API_MISUSE,
16     correctness,
17     "various things that will negatively affect your serde experience"
18 }
19
20
21 #[derive(Copy, Clone)]
22 pub struct Serde;
23
24 impl LintPass for Serde {
25     fn get_lints(&self) -> LintArray {
26         lint_array!(SERDE_API_MISUSE)
27     }
28 }
29
30 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Serde {
31     fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
32         if let ItemImpl(_, _, _, _, Some(ref trait_ref), _, ref items) = item.node {
33             let did = trait_ref.path.def.def_id();
34             if let Some(visit_did) = get_trait_def_id(cx, &paths::SERDE_DE_VISITOR) {
35                 if did == visit_did {
36                     let mut seen_str = None;
37                     let mut seen_string = None;
38                     for item in items {
39                         match &*item.name.as_str() {
40                             "visit_str" => seen_str = Some(item.span),
41                             "visit_string" => seen_string = Some(item.span),
42                             _ => {},
43                         }
44                     }
45                     if let Some(span) = seen_string {
46                         if seen_str.is_none() {
47                             span_lint(
48                                 cx,
49                                 SERDE_API_MISUSE,
50                                 span,
51                                 "you should not implement `visit_string` without also implementing `visit_str`",
52                             );
53                         }
54                     }
55                 }
56             }
57         }
58     }
59 }