1 use clippy_utils::diagnostics::span_lint;
2 use clippy_utils::{get_trait_def_id, paths};
3 use rustc_hir::{Impl, Item, ItemKind};
4 use rustc_lint::{LateContext, LateLintPass};
5 use rustc_session::{declare_lint_pass, declare_tool_lint};
9 /// Checks for mis-uses of the serde API.
11 /// ### Why is this bad?
12 /// Serde is very finnicky about how its API should be
13 /// used, but the type system can't be used to enforce it (yet?).
16 /// Implementing `Visitor::visit_string` but not
17 /// `Visitor::visit_str`.
20 "various things that will negatively affect your serde experience"
23 declare_lint_pass!(SerdeApi => [SERDE_API_MISUSE]);
25 impl<'tcx> LateLintPass<'tcx> for SerdeApi {
26 fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) {
27 if let ItemKind::Impl(Impl {
28 of_trait: Some(ref trait_ref),
33 let did = trait_ref.path.res.def_id();
34 if let Some(visit_did) = get_trait_def_id(cx, &paths::SERDE_DE_VISITOR) {
36 let mut seen_str = None;
37 let mut seen_string = None;
39 match &*item.ident.as_str() {
40 "visit_str" => seen_str = Some(item.span),
41 "visit_string" => seen_string = Some(item.span),
45 if let Some(span) = seen_string {
46 if seen_str.is_none() {
51 "you should not implement `visit_string` without also implementing `visit_str`",