Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ pub fn register_lint_passes(store: &mut rustc_lint::LintStore, conf: &'static Co
Box::new(|_| Box::<unused_async::UnusedAsync>::default()),
Box::new(move |tcx| Box::new(disallowed_types::DisallowedTypes::new(tcx, conf))),
Box::new(move |tcx| Box::new(missing_enforced_import_rename::ImportRename::new(tcx, conf))),
Box::new(|_| Box::new(strlen_on_c_strings::StrlenOnCStrings)),
Box::new(move |_| Box::new(strlen_on_c_strings::StrlenOnCStrings::new(conf))),
Box::new(move |_| Box::new(self_named_constructors::SelfNamedConstructors)),
Box::new(move |_| Box::new(iter_not_returning_iterator::IterNotReturningIterator)),
Box::new(move |_| Box::new(manual_assert::ManualAssert)),
Expand Down
31 changes: 25 additions & 6 deletions clippy_lints/src/strlen_on_c_strings.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
use clippy_config::Conf;
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::msrvs::{self, Msrv};
use clippy_utils::res::MaybeDef;
use clippy_utils::source::snippet_with_context;
use clippy_utils::visitors::is_expr_unsafe;
use clippy_utils::{match_libc_symbol, sym};
use rustc_errors::Applicability;
use rustc_hir::{Block, BlockCheckMode, Expr, ExprKind, LangItem, Node, UnsafeSource};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::declare_lint_pass;
use rustc_session::impl_lint_pass;

declare_clippy_lint! {
/// ### What it does
/// Checks for usage of `libc::strlen` on a `CString` or `CStr` value,
/// and suggest calling `as_bytes().len()` or `to_bytes().len()` respectively instead.
/// and suggest calling `count_bytes()` instead.
///
/// ### Why is this bad?
/// libc::strlen is an unsafe function, which we don't need to call
Expand All @@ -27,15 +29,25 @@ declare_clippy_lint! {
/// ```rust, no_run
/// use std::ffi::CString;
/// let cstring = CString::new("foo").expect("CString::new failed");
/// let len = cstring.as_bytes().len();
/// let len = cstring.count_bytes();
/// ```
#[clippy::version = "1.55.0"]
pub STRLEN_ON_C_STRINGS,
complexity,
"using `libc::strlen` on a `CString` or `CStr` value, while `as_bytes().len()` or `to_bytes().len()` respectively can be used instead"
"using `libc::strlen` on a `CString` or `CStr` value, while `count_bytes()` can be used instead"
}

declare_lint_pass!(StrlenOnCStrings => [STRLEN_ON_C_STRINGS]);
pub struct StrlenOnCStrings {
msrv: Msrv,
}

impl StrlenOnCStrings {
pub fn new(conf: &Conf) -> Self {
Self { msrv: conf.msrv }
}
}

impl_lint_pass!(StrlenOnCStrings => [STRLEN_ON_C_STRINGS]);

impl<'tcx> LateLintPass<'tcx> for StrlenOnCStrings {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
Expand Down Expand Up @@ -80,7 +92,14 @@ impl<'tcx> LateLintPass<'tcx> for StrlenOnCStrings {
|diag| {
let mut app = Applicability::MachineApplicable;
let val_name = snippet_with_context(cx, self_arg.span, ctxt, "_", &mut app).0;
diag.span_suggestion(span, "use", format!("{val_name}.to_bytes().len()"), app);

let suggestion = if self.msrv.meets(cx, msrvs::CSTR_COUNT_BYTES) {
format!("{val_name}.count_bytes()")
} else {
format!("{val_name}.to_bytes().len()")
};

diag.span_suggestion(span, "use", suggestion, app);
},
);
}
Expand Down
2 changes: 1 addition & 1 deletion clippy_utils/src/msrvs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ msrv_aliases! {
1,82,0 { IS_NONE_OR, REPEAT_N, RAW_REF_OP, SPECIALIZED_TO_STRING_FOR_REFS }
1,81,0 { LINT_REASONS_STABILIZATION, ERROR_IN_CORE, EXPLICIT_SELF_TYPE_ELISION, DURATION_ABS_DIFF }
1,80,0 { BOX_INTO_ITER, LAZY_CELL }
1,79,0 { CONST_BLOCKS }
1,79,0 { CONST_BLOCKS, CSTR_COUNT_BYTES }
1,77,0 { C_STR_LITERALS }
1,76,0 { PTR_FROM_REF, OPTION_RESULT_INSPECT }
1,75,0 { OPTION_AS_SLICE }
Expand Down
42 changes: 32 additions & 10 deletions tests/ui/strlen_on_c_strings.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,62 @@ use std::ffi::{CStr, CString};
fn main() {
// CString
let cstring = CString::new("foo").expect("CString::new failed");
let _ = cstring.to_bytes().len();
let _ = cstring.count_bytes();
//~^ ERROR: using `libc::strlen` on a `CString` value

// CStr
let cstr = CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul failed");
let _ = cstr.to_bytes().len();
let _ = cstr.count_bytes();
//~^ ERROR: using `libc::strlen` on a `CStr` value

let _ = cstr.to_bytes().len();
let _ = cstr.count_bytes();
//~^ ERROR: using `libc::strlen` on a `CStr` value

let pcstr: *const &CStr = &cstr;
let _ = unsafe { (*pcstr).to_bytes().len() };
let _ = unsafe { (*pcstr).count_bytes() };
//~^ ERROR: using `libc::strlen` on a `CStr` value

unsafe fn unsafe_identity<T>(x: T) -> T {
x
}
let _ = unsafe { unsafe_identity(cstr).to_bytes().len() };
let _ = unsafe { unsafe_identity(cstr).count_bytes() };
//~^ ERROR: using `libc::strlen` on a `CStr` value
let _ = unsafe { unsafe_identity(cstr) }.to_bytes().len();
let _ = unsafe { unsafe_identity(cstr) }.count_bytes();
//~^ ERROR: using `libc::strlen` on a `CStr` value

let f: unsafe fn(_) -> _ = unsafe_identity;
let _ = unsafe { f(cstr).to_bytes().len() };
let _ = unsafe { f(cstr).count_bytes() };
//~^ ERROR: using `libc::strlen` on a `CStr` value
}

// make sure we lint types that _adjust_ to `CStr`
fn adjusted(box_cstring: Box<CString>, box_cstr: Box<CStr>, arc_cstring: std::sync::Arc<CStr>) {
let _ = box_cstring.to_bytes().len();
let _ = box_cstring.count_bytes();
//~^ ERROR: using `libc::strlen` on a type that dereferences to `CStr`
let _ = box_cstr.to_bytes().len();
let _ = box_cstr.count_bytes();
//~^ ERROR: using `libc::strlen` on a type that dereferences to `CStr`
let _ = arc_cstring.to_bytes().len();
let _ = arc_cstring.count_bytes();
//~^ ERROR: using `libc::strlen` on a type that dereferences to `CStr`
}

#[clippy::msrv = "1.78"]
fn msrv_1_78() {
let cstr = CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul failed");
let _ = cstr.to_bytes().len();
//~^ strlen_on_c_strings

let cstring = CString::new("foo").expect("CString::new failed");
let _ = cstring.to_bytes().len();
//~^ strlen_on_c_strings
}

#[clippy::msrv = "1.79"]
fn msrv_1_79() {
let cstr = CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul failed");
let _ = cstr.count_bytes();
//~^ strlen_on_c_strings

let cstring = CString::new("foo").expect("CString::new failed");
let _ = cstring.count_bytes();
//~^ strlen_on_c_strings
}
22 changes: 22 additions & 0 deletions tests/ui/strlen_on_c_strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,25 @@ fn adjusted(box_cstring: Box<CString>, box_cstr: Box<CStr>, arc_cstring: std::sy
let _ = unsafe { libc::strlen(arc_cstring.as_ptr()) };
//~^ ERROR: using `libc::strlen` on a type that dereferences to `CStr`
}

#[clippy::msrv = "1.78"]
fn msrv_1_78() {
let cstr = CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul failed");
let _ = unsafe { libc::strlen(cstr.as_ptr()) };
//~^ strlen_on_c_strings

let cstring = CString::new("foo").expect("CString::new failed");
let _ = unsafe { libc::strlen(cstring.as_ptr()) };
//~^ strlen_on_c_strings
}

#[clippy::msrv = "1.79"]
fn msrv_1_79() {
let cstr = CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul failed");
let _ = unsafe { libc::strlen(cstr.as_ptr()) };
//~^ strlen_on_c_strings

let cstring = CString::new("foo").expect("CString::new failed");
let _ = unsafe { libc::strlen(cstring.as_ptr()) };
//~^ strlen_on_c_strings
}
46 changes: 35 additions & 11 deletions tests/ui/strlen_on_c_strings.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error: using `libc::strlen` on a `CString` value
--> tests/ui/strlen_on_c_strings.rs:10:13
|
LL | let _ = unsafe { libc::strlen(cstring.as_ptr()) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `cstring.to_bytes().len()`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `cstring.count_bytes()`
|
= note: `-D clippy::strlen-on-c-strings` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::strlen_on_c_strings)]`
Expand All @@ -11,55 +11,79 @@ error: using `libc::strlen` on a `CStr` value
--> tests/ui/strlen_on_c_strings.rs:15:13
|
LL | let _ = unsafe { libc::strlen(cstr.as_ptr()) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `cstr.to_bytes().len()`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `cstr.count_bytes()`

error: using `libc::strlen` on a `CStr` value
--> tests/ui/strlen_on_c_strings.rs:18:13
|
LL | let _ = unsafe { strlen(cstr.as_ptr()) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `cstr.to_bytes().len()`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `cstr.count_bytes()`

error: using `libc::strlen` on a `CStr` value
--> tests/ui/strlen_on_c_strings.rs:22:22
|
LL | let _ = unsafe { strlen((*pcstr).as_ptr()) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `(*pcstr).to_bytes().len()`
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `(*pcstr).count_bytes()`

error: using `libc::strlen` on a `CStr` value
--> tests/ui/strlen_on_c_strings.rs:28:22
|
LL | let _ = unsafe { strlen(unsafe_identity(cstr).as_ptr()) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `unsafe_identity(cstr).to_bytes().len()`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `unsafe_identity(cstr).count_bytes()`

error: using `libc::strlen` on a `CStr` value
--> tests/ui/strlen_on_c_strings.rs:30:13
|
LL | let _ = unsafe { strlen(unsafe { unsafe_identity(cstr) }.as_ptr()) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `unsafe { unsafe_identity(cstr) }.to_bytes().len()`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `unsafe { unsafe_identity(cstr) }.count_bytes()`

error: using `libc::strlen` on a `CStr` value
--> tests/ui/strlen_on_c_strings.rs:34:22
|
LL | let _ = unsafe { strlen(f(cstr).as_ptr()) };
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `f(cstr).to_bytes().len()`
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `f(cstr).count_bytes()`

error: using `libc::strlen` on a type that dereferences to `CStr`
--> tests/ui/strlen_on_c_strings.rs:40:13
|
LL | let _ = unsafe { libc::strlen(box_cstring.as_ptr()) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `box_cstring.to_bytes().len()`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `box_cstring.count_bytes()`

error: using `libc::strlen` on a type that dereferences to `CStr`
--> tests/ui/strlen_on_c_strings.rs:42:13
|
LL | let _ = unsafe { libc::strlen(box_cstr.as_ptr()) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `box_cstr.to_bytes().len()`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `box_cstr.count_bytes()`

error: using `libc::strlen` on a type that dereferences to `CStr`
--> tests/ui/strlen_on_c_strings.rs:44:13
|
LL | let _ = unsafe { libc::strlen(arc_cstring.as_ptr()) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `arc_cstring.to_bytes().len()`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `arc_cstring.count_bytes()`

error: using `libc::strlen` on a `CStr` value
--> tests/ui/strlen_on_c_strings.rs:51:13
|
LL | let _ = unsafe { libc::strlen(cstr.as_ptr()) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `cstr.to_bytes().len()`

error: using `libc::strlen` on a `CString` value
--> tests/ui/strlen_on_c_strings.rs:55:13
|
LL | let _ = unsafe { libc::strlen(cstring.as_ptr()) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `cstring.to_bytes().len()`

error: using `libc::strlen` on a `CStr` value
--> tests/ui/strlen_on_c_strings.rs:62:13
|
LL | let _ = unsafe { libc::strlen(cstr.as_ptr()) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `cstr.count_bytes()`

error: using `libc::strlen` on a `CString` value
--> tests/ui/strlen_on_c_strings.rs:66:13
|
LL | let _ = unsafe { libc::strlen(cstring.as_ptr()) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `cstring.count_bytes()`

error: aborting due to 10 previous errors
error: aborting due to 14 previous errors