-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathmultiple_bound_locations.rs
More file actions
86 lines (80 loc) · 3 KB
/
multiple_bound_locations.rs
File metadata and controls
86 lines (80 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use rustc_ast::visit::FnKind;
use rustc_ast::{Fn, NodeId, WherePredicateKind};
use rustc_data_structures::fx::FxHashMap;
use rustc_lint::{EarlyContext, EarlyLintPass};
use rustc_session::declare_lint_pass;
use rustc_span::Span;
use clippy_utils::diagnostics::span_lint;
use clippy_utils::source::SpanRangeExt;
declare_clippy_lint! {
/// ### What it does
/// Check if a generic is defined both in the bound predicate and in the `where` clause.
///
/// ### Why is this bad?
/// It can be confusing for developers when seeing bounds for a generic in multiple places.
///
/// ### Example
/// ```no_run
/// fn ty<F: std::fmt::Debug>(a: F)
/// where
/// F: Sized,
/// {}
/// ```
/// Use instead:
/// ```no_run
/// fn ty<F>(a: F)
/// where
/// F: Sized + std::fmt::Debug,
/// {}
/// ```
#[clippy::version = "1.78.0"]
pub MULTIPLE_BOUND_LOCATIONS,
style,
"defining generic bounds in multiple locations"
}
declare_lint_pass!(MultipleBoundLocations => [MULTIPLE_BOUND_LOCATIONS]);
impl EarlyLintPass for MultipleBoundLocations {
fn check_fn(&mut self, cx: &EarlyContext<'_>, kind: FnKind<'_>, _: Span, _: NodeId) {
if let FnKind::Fn(_, _, Fn { generics, .. }) = kind
&& !generics.params.is_empty()
&& !generics.where_clause.predicates.is_empty()
{
let mut generic_params_with_bounds = FxHashMap::default();
for param in &generics.params {
if !param.bounds.is_empty() {
generic_params_with_bounds.insert(param.ident.as_str(), param.ident.span);
}
}
for clause in &generics.where_clause.predicates {
match &clause.kind {
WherePredicateKind::BoundPredicate(pred) => {
if (!pred.bound_generic_params.is_empty() || !pred.bounds.is_empty())
&& let Some(Some(bound_span)) = pred
.bounded_ty
.span
.with_source_text(cx, |src| generic_params_with_bounds.get(src))
{
emit_lint(cx, *bound_span, pred.bounded_ty.span);
}
},
WherePredicateKind::RegionPredicate(pred) => {
if !pred.bounds.is_empty()
&& let Some(bound_span) = generic_params_with_bounds.get(&pred.lifetime.ident.as_str())
{
emit_lint(cx, *bound_span, pred.lifetime.ident.span);
}
},
WherePredicateKind::EqPredicate(_) => {},
}
}
}
}
}
fn emit_lint(cx: &EarlyContext<'_>, bound_span: Span, where_span: Span) {
span_lint(
cx,
MULTIPLE_BOUND_LOCATIONS,
vec![bound_span, where_span],
"bound is defined in more than one place",
);
}
