Skip to content

Commit 5d60f9f

Browse files
committed
Refactor and update test.
1 parent e8b2711 commit 5d60f9f

File tree

4 files changed

+52
-36
lines changed

4 files changed

+52
-36
lines changed

clippy_lints/src/from_instead_of_into.rs

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use rustc_hir::*;
66
use rustc_lint::{LateContext, LateLintPass};
77
use rustc_session::{declare_lint_pass, declare_tool_lint};
88
use rustc_span::symbol::sym;
9+
use std::borrow::Cow;
910

1011
declare_clippy_lint! {
1112
/// **What it does:** Checking for using of From or TryFrom trait as a generic bound.
@@ -37,7 +38,7 @@ declare_lint_pass!(FromInsteadOfInto => [FROM_INSTEAD_OF_INTO]);
3738

3839
impl LateLintPass<'tcx> for FromInsteadOfInto {
3940
fn check_where_predicate(&mut self, cx: &LateContext<'tcx>, wp: &'tcx WherePredicate<'tcx>) {
40-
let is_target_generic_bound = |b: &GenericBound<'_>| {
41+
fn is_target_generic_bound(cx: &LateContext<'tcx>, b: &GenericBound<'_>) -> bool {
4142
if_chain! {
4243
if let Some(r) = b.trait_ref();
4344
if let Some(def_id) = r.trait_def_id();
@@ -48,20 +49,56 @@ impl LateLintPass<'tcx> for FromInsteadOfInto {
4849
false
4950
}
5051
}
51-
};
52+
}
53+
54+
fn get_reduced_bounds_str(
55+
cx: &LateContext<'tcx>,
56+
position: usize,
57+
bounds: GenericBounds<'tcx>,
58+
) -> Cow<'tcx, str> {
59+
let before;
60+
if position == 0 {
61+
before = None;
62+
} else {
63+
let first_bound = &bounds[0];
64+
let previous_bound = &bounds[position - 1];
65+
before = Some(snippet(
66+
cx,
67+
first_bound.span().with_hi(previous_bound.span().hi()),
68+
"..",
69+
));
70+
}
71+
72+
let after;
73+
let last_position = bounds.len() - 1;
74+
if position == last_position {
75+
after = None;
76+
} else {
77+
let last_bound = &bounds[last_position];
78+
let after_bound = &bounds[position + 1];
79+
after = Some(snippet(cx, after_bound.span().with_hi(last_bound.span().hi()), ".."));
80+
}
81+
82+
match (before, after) {
83+
(None, None) => unreachable!(),
84+
(Some(b), None) => b,
85+
(None, Some(a)) => a,
86+
(Some(b), Some(a)) => b + " + " + a,
87+
}
88+
}
89+
5290
match wp {
5391
WherePredicate::BoundPredicate(wbp) => {
5492
if_chain! {
5593
let bounds = wbp.bounds;
56-
if let Some(position) = bounds.iter().position(|b| is_target_generic_bound(b));
94+
if let Some(position) = bounds.iter().position(|b| is_target_generic_bound(cx, b));
5795
let target_bound = &bounds[position];
5896
if let Some(tr_ref) = target_bound.trait_ref();
5997
if let Some(def_id) = tr_ref.trait_def_id();
6098
if let Some(last_seg) = tr_ref.path.segments.last();
6199
if let Some(generic_arg) = last_seg.args().args.first();
62100
if let Some(bounded_ty) = snippet_opt(cx, wbp.bounded_ty.span);
63101
if let Some(generic_arg_of_from_or_try_from) = snippet_opt(cx, generic_arg.span());
64-
// if wbp.bounds.len() == 1;
65102
then {
66103
let replace_trait_name;
67104
let target_trait_name;
@@ -84,33 +121,8 @@ impl LateLintPass<'tcx> for FromInsteadOfInto {
84121
if bounds.len() == 1 {
85122
sugg = extracted_where_predicate;
86123
} else {
87-
let before;
88-
if position == 0 {
89-
before = None;
90-
} else {
91-
let first_bound = &bounds[0];
92-
let previous_bound = &bounds[position-1];
93-
before = Some(snippet(cx, first_bound.span().with_hi(previous_bound.span().hi()), ".."));
94-
}
95-
96-
let after;
97-
let last_position = bounds.len() - 1;
98-
if position == last_position {
99-
after = None;
100-
} else {
101-
let last_bound = &bounds[last_position];
102-
let after_bound = &bounds[position + 1];
103-
after = Some(snippet(cx, after_bound.span().with_hi(last_bound.span().hi()), ".."));
104-
}
105-
106-
let bounds_str = match (before, after) {
107-
(None, None) => unreachable!(),
108-
(Some(b), None) => b,
109-
(None, Some(a)) => a,
110-
(Some(b), Some(a)) => b + " + " + a,
111-
};
112-
113-
sugg = format!("{}, {}: {}", extracted_where_predicate, bounded_ty, bounds_str);
124+
let bounds = get_reduced_bounds_str(cx, position, bounds);
125+
sugg = format!("{}, {}: {}", extracted_where_predicate, bounded_ty, bounds);
114126
}
115127
span_lint_and_sugg(
116128
cx,

tests/ui/from_instead_of_into.fixed

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// run-rustfix
22
#![allow(unused_imports)]
3+
#![allow(unused_variables)]
4+
#![allow(dead_code)]
35
#![warn(clippy::from_instead_of_into)]
46
use std::convert::TryFrom;
57
use std::convert::TryInto;

tests/ui/from_instead_of_into.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// run-rustfix
22
#![allow(unused_imports)]
3+
#![allow(unused_variables)]
4+
#![allow(dead_code)]
35
#![warn(clippy::from_instead_of_into)]
46
use std::convert::TryFrom;
57
use std::convert::TryInto;

tests/ui/from_instead_of_into.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
error: Into trait is preferable than From as a generic bound
2-
--> $DIR/from_instead_of_into.rs:9:5
2+
--> $DIR/from_instead_of_into.rs:11:5
33
|
44
LL | u32: From<T>,
55
| ^^^^^^^^^^^^ help: try: `T: Into<u32>`
66
|
77
= note: `-D clippy::from-instead-of-into` implied by `-D warnings`
88

99
error: Into trait is preferable than From as a generic bound
10-
--> $DIR/from_instead_of_into.rs:15:5
10+
--> $DIR/from_instead_of_into.rs:17:5
1111
|
1212
LL | u32: Copy + Clone + From<T>,
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `T: Into<u32>, u32: Copy + Clone`
1414

1515
error: TryInto trait is preferable than TryFrom as a generic bound
16-
--> $DIR/from_instead_of_into.rs:21:5
16+
--> $DIR/from_instead_of_into.rs:23:5
1717
|
1818
LL | u32: TryFrom<T>,
1919
| ^^^^^^^^^^^^^^^ help: try: `T: TryInto<u32>`
2020

2121
error: TryInto trait is preferable than TryFrom as a generic bound
22-
--> $DIR/from_instead_of_into.rs:27:5
22+
--> $DIR/from_instead_of_into.rs:29:5
2323
|
2424
LL | u32: Copy + TryFrom<T> + Clone,
2525
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `T: TryInto<u32>, u32: Copy + Clone`
2626

2727
error: TryInto trait is preferable than TryFrom as a generic bound
28-
--> $DIR/from_instead_of_into.rs:33:5
28+
--> $DIR/from_instead_of_into.rs:35:5
2929
|
3030
LL | u32: TryFrom<T> + Copy + Clone,
3131
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `T: TryInto<u32>, u32: Copy + Clone`

0 commit comments

Comments
 (0)