-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
7 changed files
with
174 additions
and
0 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
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,86 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::source::snippet_opt; | ||
use rustc_errors::Applicability; | ||
use rustc_hir as hir; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_session::declare_lint_pass; | ||
use rustc_span::symbol::sym; | ||
use rustc_span::Span; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Detects needlessly owned `Cow` types. | ||
/// | ||
/// ### Why is this bad? | ||
/// The borrowed types are usually more flexible (e.g. `&str` vs. `&String`). | ||
/// | ||
/// ### Example | ||
/// ```no_run | ||
/// let wrogn: std::borrow::Cow<'_, String>; | ||
/// ``` | ||
/// Use instead: | ||
/// ```no_run | ||
/// let right: std::borrow::Cow<'_, str>; | ||
/// ``` | ||
#[clippy::version = "1.85.0"] | ||
pub OWNED_COW, | ||
style, | ||
"needlessly owned Cow type" | ||
} | ||
|
||
declare_lint_pass!(TooOwned => [OWNED_COW]); | ||
|
||
impl LateLintPass<'_> for TooOwned { | ||
//TODO: Switch to splitting between in-body and out-of-body execution | ||
//typeck_results() exists only in-body | ||
fn check_ty(&mut self, cx: &LateContext<'_>, ty: &hir::Ty<'_>) { | ||
if let hir::TyKind::Path(qpath) = ty.kind | ||
&& let Some(cow_def_id) = cx.qpath_res(&qpath, ty.hir_id).opt_def_id() | ||
&& cx.tcx.is_diagnostic_item(sym::Cow, cow_def_id) | ||
&& let hir::QPath::Resolved(_, path) = qpath | ||
&& let [.., last_seg] = path.segments | ||
&& let Some(args) = last_seg.args | ||
&& let [_lt, carg] = args.args | ||
&& let hir::GenericArg::Type(cty) = carg | ||
&& let Some((span, repl)) = replacement(cx, cty) | ||
{ | ||
span_lint_and_sugg( | ||
cx, | ||
OWNED_COW, | ||
span, | ||
"needlessly owned Cow type", | ||
"use", | ||
repl, | ||
Applicability::MachineApplicable, | ||
); | ||
} | ||
} | ||
} | ||
|
||
fn replacement(cx: &LateContext<'_>, cty: &hir::Ty<'_>) -> Option<(Span, String)> { | ||
if clippy_utils::is_path_lang_item(cx, cty, hir::LangItem::String) { | ||
return Some((cty.span, "str".into())); | ||
} | ||
if clippy_utils::is_path_diagnostic_item(cx, cty, sym::Vec) { | ||
return if let hir::TyKind::Path(hir::QPath::Resolved(_, path)) = cty.kind | ||
&& let [.., last_seg] = path.segments | ||
&& let Some(args) = last_seg.args | ||
&& let [t, ..] = args.args | ||
&& let Some(snip) = snippet_opt(cx, t.span()) | ||
{ | ||
Some((cty.span, format!("[{snip}]"))) | ||
} else { | ||
None | ||
}; | ||
} | ||
for (diag, repl) in [ | ||
(sym::cstring_type, "std::ffi::CStr"), | ||
(sym::OsString, "std::ffi::OsStr"), | ||
(sym::PathBuf, "std::path::Path"), | ||
] { | ||
if clippy_utils::is_path_diagnostic_item(cx, cty, diag) { | ||
return Some((cty.span, repl.into())); | ||
} | ||
} | ||
None | ||
} |
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,17 @@ | ||
#![warn(clippy::cmp_owned)] | ||
|
||
use std::borrow::Cow; | ||
use std::ffi::{CString, OsString}; | ||
use std::path::PathBuf; | ||
|
||
fn main() { | ||
let x: Cow<'static, str> = Cow::Owned(String::from("Hi!")); | ||
let y: Cow<'_, [u8]> = Cow::Owned(vec![]); | ||
let z: Cow<'_, [_]> = Cow::Owned(vec![2_i32]); | ||
let o: Cow<'_, std::ffi::OsStr> = Cow::Owned(OsString::new()); | ||
let c: Cow<'_, std::ffi::CStr> = Cow::Owned(CString::new("").unwrap()); | ||
let p: Cow<'_, std::path::Path> = Cow::Owned(PathBuf::new()); | ||
|
||
// false positive: borrowed type | ||
let b: Cow<'_, str> = Cow::Borrowed("Hi!"); | ||
} |
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,17 @@ | ||
#![warn(clippy::too_owned)] | ||
|
||
use std::borrow::Cow; | ||
use std::ffi::{CString, OsString}; | ||
use std::path::PathBuf; | ||
|
||
fn main() { | ||
let x: Cow<'static, String> = Cow::Owned(String::from("Hi!")); | ||
let y: Cow<'_, Vec<u8>> = Cow::Owned(vec![]); | ||
let z: Cow<'_, Vec<_>> = Cow::Owned(vec![2_i32]); | ||
let o: Cow<'_, OsString> = Cow::Owned(OsString::new()); | ||
let c: Cow<'_, CString> = Cow::Owned(CString::new("").unwrap()); | ||
let p: Cow<'_, PathBuf> = Cow::Owned(PathBuf::new()); | ||
|
||
// false positive: borrowed type | ||
let b: Cow<'_, str> = Cow::Borrowed("Hi!"); | ||
} |
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 @@ | ||
error: unknown lint: `clippy::too_owned` | ||
--> tests/ui/too_owned.rs:1:9 | ||
| | ||
LL | #![warn(clippy::too_owned)] | ||
| ^^^^^^^^^^^^^^^^^ help: did you mean: `clippy::cmp_owned` | ||
| | ||
= note: `-D unknown-lints` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(unknown_lints)]` | ||
|
||
error: needlessly owned Cow type | ||
--> tests/ui/too_owned.rs:8:25 | ||
| | ||
LL | let x: Cow<'static, String> = Cow::Owned(String::from("Hi!")); | ||
| ^^^^^^ help: use: `str` | ||
| | ||
= note: `-D clippy::owned-cow` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::owned_cow)]` | ||
|
||
error: needlessly owned Cow type | ||
--> tests/ui/too_owned.rs:9:20 | ||
| | ||
LL | let y: Cow<'_, Vec<u8>> = Cow::Owned(vec![]); | ||
| ^^^^^^^ help: use: `[u8]` | ||
|
||
error: needlessly owned Cow type | ||
--> tests/ui/too_owned.rs:10:20 | ||
| | ||
LL | let z: Cow<'_, Vec<_>> = Cow::Owned(vec![2_i32]); | ||
| ^^^^^^ help: use: `[_]` | ||
|
||
error: needlessly owned Cow type | ||
--> tests/ui/too_owned.rs:11:20 | ||
| | ||
LL | let o: Cow<'_, OsString> = Cow::Owned(OsString::new()); | ||
| ^^^^^^^^ help: use: `std::ffi::OsStr` | ||
|
||
error: needlessly owned Cow type | ||
--> tests/ui/too_owned.rs:12:20 | ||
| | ||
LL | let c: Cow<'_, CString> = Cow::Owned(CString::new("").unwrap()); | ||
| ^^^^^^^ help: use: `std::ffi::CStr` | ||
|
||
error: needlessly owned Cow type | ||
--> tests/ui/too_owned.rs:13:20 | ||
| | ||
LL | let p: Cow<'_, PathBuf> = Cow::Owned(PathBuf::new()); | ||
| ^^^^^^^ help: use: `std::path::Path` | ||
|
||
error: aborting due to 7 previous errors | ||
|