Skip to content

Commit aa0a94d

Browse files
committed
Update useless_conversion and box_collection clippy lints to use String type alias diagnostic item.
1 parent f38ea3b commit aa0a94d

File tree

4 files changed

+17
-2
lines changed

4 files changed

+17
-2
lines changed

library/alloc/src/string.rs

+1
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ pub mod string;
343343
/// [`Deref`]: core::ops::Deref "ops::Deref"
344344
/// [`as_str()`]: String::as_str
345345
#[stable(feature = "rust1", since = "1.0.0")]
346+
#[cfg_attr(not(test), rustc_diagnostic_item = "String")]
346347
pub type String = string::String<Global>;
347348

348349
/// A possible error value when converting a `String` from a UTF-8 byte vector.

src/tools/clippy/clippy_lints/src/types/box_collection.rs

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ fn get_std_collection(cx: &LateContext<'_>, qpath: &QPath<'_>) -> Option<Symbol>
4747
| sym::BTreeMap
4848
| sym::BTreeSet
4949
| sym::BinaryHeap
50+
| sym::String
5051
)
5152
})
5253
.or_else(|| {

src/tools/clippy/clippy_lints/src/useless_conversion.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ use clippy_utils::source::{snippet, snippet_with_context};
33
use clippy_utils::sugg::{DiagExt as _, Sugg};
44
use clippy_utils::ty::{is_copy, is_type_diagnostic_item, same_type_and_consts};
55
use clippy_utils::{
6-
get_parent_expr, is_inherent_method_call, is_trait_item, is_trait_method, is_ty_alias, path_to_local,
6+
get_parent_expr, is_inherent_method_call, is_string_ty_alias, is_trait_item, is_trait_method, is_ty_alias,
7+
path_to_local,
78
};
89
use rustc_errors::Applicability;
910
use rustc_hir::def_id::DefId;
@@ -333,7 +334,7 @@ impl<'tcx> LateLintPass<'tcx> for UselessConversion {
333334
ExprKind::Call(path, [arg]) => {
334335
if let ExprKind::Path(ref qpath) = path.kind
335336
&& let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id()
336-
&& !is_ty_alias(qpath)
337+
&& (!is_ty_alias(qpath) || is_string_ty_alias(cx, qpath))
337338
{
338339
let a = cx.typeck_results().expr_ty(e);
339340
let b = cx.typeck_results().expr_ty(arg);

src/tools/clippy/clippy_utils/src/lib.rs

+12
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,18 @@ pub fn is_ty_alias(qpath: &QPath<'_>) -> bool {
350350
}
351351
}
352352

353+
/// Checks if the given `QPath` belongs to the specific type alias
354+
/// `std::string::String`/`alloc::string::String`.
355+
pub fn is_string_ty_alias(cx: &LateContext<'_>, qpath: &QPath<'_>) -> bool {
356+
match *qpath {
357+
QPath::Resolved(_, path) => {
358+
matches!(path.res, Res::Def(DefKind::TyAlias, id) if cx.tcx.get_diagnostic_name(id) == Some(sym::String))
359+
},
360+
QPath::TypeRelative(ty, _) if let TyKind::Path(qpath) = ty.kind => is_string_ty_alias(cx, &qpath),
361+
_ => false,
362+
}
363+
}
364+
353365
/// Checks if the method call given in `expr` belongs to the given trait.
354366
/// This is a deprecated function, consider using [`is_trait_method`].
355367
pub fn match_trait_method(cx: &LateContext<'_>, expr: &Expr<'_>, path: &[&str]) -> bool {

0 commit comments

Comments
 (0)