Skip to content

Commit 454dcab

Browse files
committed
Added the new lint with some docs
1 parent b15f06e commit 454dcab

File tree

9 files changed

+54
-2
lines changed

9 files changed

+54
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3989,6 +3989,7 @@ Released 2018-09-13
39893989
[`unstable_as_mut_slice`]: https://rust-lang.github.io/rust-clippy/master/index.html#unstable_as_mut_slice
39903990
[`unstable_as_slice`]: https://rust-lang.github.io/rust-clippy/master/index.html#unstable_as_slice
39913991
[`unused_async`]: https://rust-lang.github.io/rust-clippy/master/index.html#unused_async
3992+
[`unused_box`]: https://rust-lang.github.io/rust-clippy/master/index.html#unused_box
39923993
[`unused_collect`]: https://rust-lang.github.io/rust-clippy/master/index.html#unused_collect
39933994
[`unused_io_amount`]: https://rust-lang.github.io/rust-clippy/master/index.html#unused_io_amount
39943995
[`unused_label`]: https://rust-lang.github.io/rust-clippy/master/index.html#unused_label

clippy_lints/src/lib.register_lints.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,7 @@ store.register_lints(&[
561561
unnested_or_patterns::UNNESTED_OR_PATTERNS,
562562
unsafe_removed_from_name::UNSAFE_REMOVED_FROM_NAME,
563563
unused_async::UNUSED_ASYNC,
564+
unused_box::UNUSED_BOX,
564565
unused_io_amount::UNUSED_IO_AMOUNT,
565566
unused_rounding::UNUSED_ROUNDING,
566567
unused_self::UNUSED_SELF,

clippy_lints/src/lib.register_nursery.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ store.register_group(true, "clippy::nursery", Some("clippy_nursery"), vec![
3131
LintId::of(trait_bounds::TRAIT_DUPLICATION_IN_BOUNDS),
3232
LintId::of(trait_bounds::TYPE_REPETITION_IN_BOUNDS),
3333
LintId::of(transmute::TRANSMUTE_UNDEFINED_REPR),
34+
LintId::of(unused_box::UNUSED_BOX),
3435
LintId::of(unused_rounding::UNUSED_ROUNDING),
3536
LintId::of(use_self::USE_SELF),
3637
])

clippy_lints/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@ mod unnecessary_wraps;
393393
mod unnested_or_patterns;
394394
mod unsafe_removed_from_name;
395395
mod unused_async;
396+
mod unused_box;
396397
mod unused_io_amount;
397398
mod unused_rounding;
398399
mod unused_self;
@@ -915,6 +916,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
915916
let verbose_bit_mask_threshold = conf.verbose_bit_mask_threshold;
916917
store.register_late_pass(move || Box::new(operators::Operators::new(verbose_bit_mask_threshold)));
917918
store.register_late_pass(|| Box::new(invalid_utf8_in_unchecked::InvalidUtf8InUnchecked));
919+
store.register_late_pass(|| Box::new(unused_box::UnusedBox));
918920
// add lints here, do not remove this comment, it's used in `new_lint`
919921
}
920922

clippy_lints/src/unused_box.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use rustc_lint::LateLintPass;
2+
use rustc_session::{declare_lint_pass, declare_tool_lint};
3+
4+
declare_clippy_lint! {
5+
/// ### What it does
6+
///
7+
/// Checks for a return type containing a `Box<T>` where `T` implements `Sized`
8+
///
9+
/// ### Why is this bad?
10+
///
11+
/// It's better to just return `T` in these cases
12+
///
13+
/// ### Example
14+
/// ```rust
15+
/// fn foo() -> Box<String> {
16+
/// Box::new(String::from("Hello, world!"))
17+
/// }
18+
/// ```
19+
/// Use instead:
20+
/// ```rust
21+
/// fn foo() -> String {
22+
/// String::from("Hello, world!")
23+
/// }
24+
/// ```
25+
#[clippy::version = "1.64.0"]
26+
pub UNUSED_BOX,
27+
nursery,
28+
"default lint description"
29+
}
30+
declare_lint_pass!(UnusedBox => [UNUSED_BOX]);
31+
32+
impl LateLintPass<'_> for UnusedBox {}

tests/ui/unknown_clippy_lints.fixed

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// Shouldn't suggest rustc lint name(`dead_code`)
1313
#[warn(clippy::drop_copy)]
1414
// Shouldn't suggest removed/deprecated clippy lint name(`unused_collect`)
15-
#[warn(clippy::unused_self)]
15+
#[warn(clippy::unused_box)]
1616
// Shouldn't suggest renamed clippy lint name(`const_static_lifetime`)
1717
#[warn(clippy::redundant_static_lifetimes)]
1818
fn main() {}

tests/ui/unknown_clippy_lints.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ error: unknown lint: `clippy::unused_colle`
4040
--> $DIR/unknown_clippy_lints.rs:15:8
4141
|
4242
LL | #[warn(clippy::unused_colle)]
43-
| ^^^^^^^^^^^^^^^^^^^^ help: did you mean: `clippy::unused_self`
43+
| ^^^^^^^^^^^^^^^^^^^^ help: did you mean: `clippy::unused_box`
4444

4545
error: unknown lint: `clippy::const_static_lifetim`
4646
--> $DIR/unknown_clippy_lints.rs:17:8

tests/ui/unused_box.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#![warn(clippy::unused_box)]
2+
3+
fn main() {
4+
// test code goes here
5+
}

tests/ui/unused_box.stderr

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: function returns `Box<usize>` when `usize` implements `Sized`
2+
--> $DIR/unused_box.rs:3:13
3+
|
4+
LL | fn foo() -> Box<usize> {
5+
| ^^^^^^^^^^ help: change the return type to: `usize`
6+
|
7+
= note: `-D clippy::unused-box` implied by `-D warnings`
8+
9+
error: aborting due to previous error
10+

0 commit comments

Comments
 (0)