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