Skip to content

Commit 5c2fc4e

Browse files
author
rudyardrichter
committed
Add lint to suggest into()
1 parent 1ba6587 commit 5c2fc4e

File tree

4 files changed

+79
-0
lines changed

4 files changed

+79
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,7 @@ All notable changes to this project will be documented in this file.
842842
[`unused_label`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#unused_label
843843
[`use_debug`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#use_debug
844844
[`use_self`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#use_self
845+
[`use_shared_from_slice`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#use_shared_from_slice
845846
[`used_underscore_binding`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#used_underscore_binding
846847
[`useless_asref`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#useless_asref
847848
[`useless_attribute`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#useless_attribute

clippy_lints/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ pub mod unsafe_removed_from_name;
200200
pub mod unused_io_amount;
201201
pub mod unused_label;
202202
pub mod use_self;
203+
pub mod use_shared_from_slice;
203204
pub mod vec;
204205
pub mod write;
205206
pub mod zero_div_zero;
@@ -914,6 +915,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
914915
mutex_atomic::MUTEX_INTEGER,
915916
needless_borrow::NEEDLESS_BORROW,
916917
ranges::RANGE_PLUS_ONE,
918+
use_shared_from_slice::USE_SHARED_FROM_SLICE,
917919
]);
918920
}
919921

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
use rustc::hir::Expr;
2+
use rustc::lint::{LateContext, LateLintPass, LintArray, LintContext, LintPass};
3+
use rustc::ty;
4+
use utils::{match_type, span_lint_and_sugg, walk_ptrs_ty};
5+
use utils::paths;
6+
7+
/// **What it does:**
8+
/// Checks for usage of `Rc<String>` or `Rc<Vec<T>>`.
9+
///
10+
/// **Why is this bad?**
11+
/// Using a `Rc<str>` or `Rc<[T]>` is more efficient and easy to construct with
12+
/// `into()`.
13+
///
14+
/// **Known problems:**
15+
/// None.
16+
///
17+
/// **Example:**
18+
///
19+
/// ```rust
20+
/// use std::rc::Rc;
21+
///
22+
/// // Bad
23+
/// let bad_ref: Rc<Vec<usize>> = Rc::new(vec!(1, 2, 3));
24+
///
25+
/// // Good
26+
/// let good_ref: Rc<[usize]> = Rc::new(vec!(1, 2, 3).into());
27+
/// ```
28+
declare_clippy_lint! {
29+
pub USE_SHARED_FROM_SLICE,
30+
nursery,
31+
"use `into()` to construct `Rc` from slice"
32+
}
33+
34+
#[derive(Copy, Clone, Debug)]
35+
pub struct Pass;
36+
37+
impl LintPass for Pass {
38+
fn get_lints(&self) -> LintArray {
39+
lint_array!(USE_SHARED_FROM_SLICE)
40+
}
41+
}
42+
43+
impl <'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
44+
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
45+
let expr_ty = walk_ptrs_ty(cx.tables.expr_ty(expr));
46+
47+
// Check for expressions with the type `Rc<Vec<T>>`.
48+
if_chain! {
49+
if let ty::TyAdt(_, subst) = expr_ty.sty;
50+
if match_type(cx, expr_ty, &paths::RC);
51+
if match_type(cx, subst.type_at(1), &paths::VEC);
52+
then {
53+
span_lint_and_sugg(
54+
cx,
55+
USE_SHARED_FROM_SLICE,
56+
expr.span,
57+
"constructing reference-counted type from vec",
58+
"consider using `into()`",
59+
format!("TODO"),
60+
);
61+
}
62+
}
63+
64+
// TODO
65+
// Check for expressions with the type `Rc<String>`.
66+
// Check for expressions with the type `Arc<String>`.
67+
// Check for expressions with the type `Arc<Vec<T>>`.
68+
}
69+
}

tests/ui/use_shared_from_slice.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
use std::rc::Rc;
2+
3+
fn main() {
4+
let bad_ref: Rc<Vec<usize>> = Rc::new(vec!(1, 2, 3));
5+
6+
let good_ref: Rc<[usize]> = vec!(1, 2, 3).into();
7+
}

0 commit comments

Comments
 (0)