-
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
9 changed files
with
205 additions
and
1 deletion.
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
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,66 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::source::snippet_opt; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::def_id::DefId; | ||
use rustc_hir::{self as hir}; | ||
use rustc_lint::LateContext; | ||
use rustc_span::{Span, sym}; | ||
|
||
pub(super) fn check(cx: &LateContext<'_>, qpath: &hir::QPath<'_>, def_id: DefId) -> bool { | ||
if cx.tcx.is_diagnostic_item(sym::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, | ||
super::OWNED_COW, | ||
span, | ||
"needlessly owned Cow type", | ||
"use", | ||
repl, | ||
Applicability::Unspecified, | ||
); | ||
return true; | ||
} | ||
false | ||
} | ||
|
||
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 | ||
}; | ||
} | ||
if clippy_utils::is_path_diagnostic_item(cx, cty, sym::cstring_type) { | ||
return Some(( | ||
cty.span, | ||
(if clippy_utils::is_no_std_crate(cx) { | ||
"core::ffi::CStr" | ||
} else { | ||
"std::ffi::CStr" | ||
}) | ||
.into(), | ||
)); | ||
} | ||
// Neither OsString nor PathBuf are available outside std | ||
for (diag, repl) in [(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::owned_cow)] | ||
|
||
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::owned_cow)] | ||
|
||
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,41 @@ | ||
error: needlessly owned Cow type | ||
--> tests/ui/owned_cow.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/owned_cow.rs:9:20 | ||
| | ||
LL | let y: Cow<'_, Vec<u8>> = Cow::Owned(vec![]); | ||
| ^^^^^^^ help: use: `[u8]` | ||
|
||
error: needlessly owned Cow type | ||
--> tests/ui/owned_cow.rs:10:20 | ||
| | ||
LL | let z: Cow<'_, Vec<_>> = Cow::Owned(vec![2_i32]); | ||
| ^^^^^^ help: use: `[_]` | ||
|
||
error: needlessly owned Cow type | ||
--> tests/ui/owned_cow.rs:11:20 | ||
| | ||
LL | let o: Cow<'_, OsString> = Cow::Owned(OsString::new()); | ||
| ^^^^^^^^ help: use: `std::ffi::OsStr` | ||
|
||
error: needlessly owned Cow type | ||
--> tests/ui/owned_cow.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/owned_cow.rs:13:20 | ||
| | ||
LL | let p: Cow<'_, PathBuf> = Cow::Owned(PathBuf::new()); | ||
| ^^^^^^^ help: use: `std::path::Path` | ||
|
||
error: aborting due to 6 previous errors | ||
|