use clippy_utils::diagnostics::span_lint_and_sugg;
-use clippy_utils::paths;
-use clippy_utils::source::snippet_with_macro_callsite;
-use clippy_utils::ty::{is_type_diagnostic_item, is_type_ref_to_diagnostic_item};
+use clippy_utils::match_libc_symbol;
+use clippy_utils::source::snippet_with_context;
+use clippy_utils::ty::is_type_diagnostic_item;
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir as hir;
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};
-use rustc_span::symbol::{sym, Symbol};
+use rustc_span::symbol::sym;
declare_clippy_lint! {
/// ### What it does
impl LateLintPass<'tcx> for StrlenOnCStrings {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
- if expr.span.from_expansion() {
- return;
- }
-
if_chain! {
+ if !expr.span.from_expansion();
if let hir::ExprKind::Call(func, [recv]) = expr.kind;
- if let hir::ExprKind::Path(hir::QPath::Resolved(_, path)) = func.kind;
-
- if (&paths::LIBC_STRLEN).iter().map(|x| Symbol::intern(x)).eq(
- path.segments.iter().map(|seg| seg.ident.name));
- if let hir::ExprKind::MethodCall(path, _, args, _) = recv.kind;
- if args.len() == 1;
- if !args.iter().any(|e| e.span.from_expansion());
+ if let hir::ExprKind::Path(path) = &func.kind;
+ if let Some(did) = cx.qpath_res(path, func.hir_id).opt_def_id();
+ if match_libc_symbol(cx, did, "strlen");
+ if let hir::ExprKind::MethodCall(path, _, [self_arg], _) = recv.kind;
+ if !recv.span.from_expansion();
if path.ident.name == sym::as_ptr;
then {
- let cstring = &args[0];
- let ty = cx.typeck_results().expr_ty(cstring);
- let val_name = snippet_with_macro_callsite(cx, cstring.span, "..");
- let sugg = if is_type_diagnostic_item(cx, ty, sym::cstring_type){
- format!("{}.as_bytes().len()", val_name)
- } else if is_type_ref_to_diagnostic_item(cx, ty, sym::CStr){
- format!("{}.to_bytes().len()", val_name)
+ let ty = cx.typeck_results().expr_ty(self_arg).peel_refs();
+ let mut app = Applicability::Unspecified;
+ let val_name = snippet_with_context(cx, self_arg.span, expr.span.ctxt(), "..", &mut app).0;
+ let method_name = if is_type_diagnostic_item(cx, ty, sym::cstring_type) {
+ "as_bytes"
+ } else if is_type_diagnostic_item(cx, ty, sym::CStr) {
+ "to_bytes"
} else {
return;
};
expr.span,
"using `libc::strlen` on a `CString` or `CStr` value",
"try this (you might also need to get rid of `unsafe` block in some cases):",
- sugg,
+ format!("{}.{}().len()", val_name, method_name),
Applicability::Unspecified // Sometimes unnecessary `unsafe` block
);
}
syms.iter().map(|x| Symbol::intern(x)).eq(path.iter().copied())
}
+/// Checks if the given `DefId` matches the `libc` item.
+pub fn match_libc_symbol(cx: &LateContext<'_>, did: DefId, name: &str) -> bool {
+ let path = cx.get_def_path(did);
+ // libc is meant to be used as a flat list of names, but they're all actually defined in different
+ // modules based on the target platform. Ignore everything but crate name and the item name.
+ path.first().map_or(false, |s| s.as_str() == "libc") && path.last().map_or(false, |s| s.as_str() == name)
+}
+
pub fn match_panic_call(cx: &LateContext<'_>, expr: &'tcx Expr<'_>) -> Option<&'tcx Expr<'tcx>> {
if let ExprKind::Call(func, [arg]) = expr.kind {
expr_path_res(cx, func)
error: using `libc::strlen` on a `CString` or `CStr` value
- --> $DIR/strlen_on_c_strings.rs:11:24
+ --> $DIR/strlen_on_c_strings.rs:12:24
|
LL | let len = unsafe { libc::strlen(cstring.as_ptr()) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ~~~~~~~~~~~~~~~~~~~~~~~~
error: using `libc::strlen` on a `CString` or `CStr` value
- --> $DIR/strlen_on_c_strings.rs:15:24
+ --> $DIR/strlen_on_c_strings.rs:16:24
|
LL | let len = unsafe { libc::strlen(cstr.as_ptr()) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL | let len = unsafe { cstr.to_bytes().len() };
| ~~~~~~~~~~~~~~~~~~~~~
-error: aborting due to 2 previous errors
+error: using `libc::strlen` on a `CString` or `CStr` value
+ --> $DIR/strlen_on_c_strings.rs:18:24
+ |
+LL | let len = unsafe { strlen(cstr.as_ptr()) };
+ | ^^^^^^^^^^^^^^^^^^^^^
+ |
+help: try this (you might also need to get rid of `unsafe` block in some cases):
+ |
+LL | let len = unsafe { cstr.to_bytes().len() };
+ | ~~~~~~~~~~~~~~~~~~~~~
+
+error: aborting due to 3 previous errors