Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
97 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use clippy_utils::diagnostics::span_lint_and_help; | ||
use clippy_utils::is_ty_param_diagnostic_item; | ||
use rustc_hir::{self as hir, def_id::DefId, QPath}; | ||
use rustc_lint::LateContext; | ||
use rustc_span::symbol::sym; | ||
|
||
use super::BOX_COLLECTION; | ||
|
||
pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_>, def_id: DefId) -> bool { | ||
if_chain! { | ||
if Some(def_id) == cx.tcx.lang_items().owned_box(); | ||
if let Some(item_type) = get_std_collection(cx, qpath); | ||
then { | ||
let generic = if item_type == "String" { | ||
"" | ||
} else { | ||
"<..>" | ||
}; | ||
span_lint_and_help( | ||
cx, | ||
BOX_COLLECTION, | ||
hir_ty.span, | ||
&format!( | ||
"you seem to be trying to use `Box<{outer}{generic}>`. Consider using just `{outer}{generic}`", | ||
outer=item_type, | ||
generic = generic), | ||
None, | ||
&format!( | ||
"`{outer}{generic}` is already on the heap, `Box<{outer}{generic}>` makes an extra allocation", | ||
outer=item_type, | ||
generic = generic) | ||
); | ||
true | ||
} else { | ||
false | ||
} | ||
} | ||
} | ||
|
||
fn get_std_collection(cx: &LateContext<'_>, qpath: &QPath<'_>) -> Option<String> { | ||
if is_ty_param_diagnostic_item(cx, qpath, sym::vec_type).is_some() { | ||
Some(String::from("Vec")) | ||
} else if is_ty_param_diagnostic_item(cx, qpath, sym::string_type).is_some() { | ||
Some(String::from("String")) | ||
} else if is_ty_param_diagnostic_item(cx, qpath, sym::hashmap_type).is_some() { | ||
Some(String::from("HashMap")) | ||
} else { | ||
None | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
error: you seem to be trying to use `Box<Vec<..>>`. Consider using just `Vec<..>` | ||
--> $DIR/box_collection.rs:21:14 | ||
| | ||
LL | fn test(foo: Box<Vec<bool>>) {} | ||
| ^^^^^^^^^^^^^^ | ||
| | ||
= note: `-D clippy::box-collection` implied by `-D warnings` | ||
= help: `Vec<..>` is already on the heap, `Box<Vec<..>>` makes an extra allocation | ||
|
||
error: you seem to be trying to use `Box<String>`. Consider using just `String` | ||
--> $DIR/box_collection.rs:28:15 | ||
| | ||
LL | fn test3(foo: Box<String>) {} | ||
| ^^^^^^^^^^^ | ||
| | ||
= help: `String` is already on the heap, `Box<String>` makes an extra allocation | ||
|
||
error: you seem to be trying to use `Box<HashMap<..>>`. Consider using just `HashMap<..>` | ||
--> $DIR/box_collection.rs:30:15 | ||
| | ||
LL | fn test4(foo: Box<HashMap<String, String>>) {} | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: `HashMap<..>` is already on the heap, `Box<HashMap<..>>` makes an extra allocation | ||
|
||
error: aborting due to 3 previous errors | ||
|
This file was deleted.
Oops, something went wrong.