Skip to content

Commit

Permalink
api adjusting & run tests & ignoring some doc-tests
Browse files Browse the repository at this point in the history
Signed-off-by: J-ZhengLi <[email protected]>
  • Loading branch information
J-ZhengLi committed Dec 1, 2023
1 parent c3f27ce commit f25fa70
Show file tree
Hide file tree
Showing 40 changed files with 168 additions and 131 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5066,6 +5066,7 @@ Released 2018-09-13
[`linkedlist`]: https://rust-lang.github.io/rust-clippy/master/index.html#linkedlist
[`little_endian_bytes`]: https://rust-lang.github.io/rust-clippy/master/index.html#little_endian_bytes
[`logic_bug`]: https://rust-lang.github.io/rust-clippy/master/index.html#logic_bug
[`loop_without_break_or_return`]: https://rust-lang.github.io/rust-clippy/master/index.html#loop_without_break_or_return
[`lossy_float_literal`]: https://rust-lang.github.io/rust-clippy/master/index.html#lossy_float_literal
[`macro_use_imports`]: https://rust-lang.github.io/rust-clippy/master/index.html#macro_use_imports
[`main_recursion`]: https://rust-lang.github.io/rust-clippy/master/index.html#main_recursion
Expand Down Expand Up @@ -5130,8 +5131,8 @@ Released 2018-09-13
[`mem_replace_option_with_none`]: https://rust-lang.github.io/rust-clippy/master/index.html#mem_replace_option_with_none
[`mem_replace_with_default`]: https://rust-lang.github.io/rust-clippy/master/index.html#mem_replace_with_default
[`mem_replace_with_uninit`]: https://rust-lang.github.io/rust-clippy/master/index.html#mem_replace_with_uninit
[`min_ident_chars`]: https://rust-lang.github.io/rust-clippy/master/index.html#min_ident_chars
[`mem_unsafe_functions`]: https://rust-lang.github.io/rust-clippy/master/index.html#mem_unsafe_functions
[`min_ident_chars`]: https://rust-lang.github.io/rust-clippy/master/index.html#min_ident_chars
[`min_max`]: https://rust-lang.github.io/rust-clippy/master/index.html#min_max
[`misaligned_transmute`]: https://rust-lang.github.io/rust-clippy/master/index.html#misaligned_transmute
[`mismatched_target_os`]: https://rust-lang.github.io/rust-clippy/master/index.html#mismatched_target_os
Expand Down
5 changes: 0 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ filetime = "0.2"
itertools = "0.10.1"
libloading = "0.8.0"

# A noop dependency that changes in the Rust repository, it's a bit of a hack.
# See the `src/tools/rustc-workspace-hack/README.md` file in `rust-lang/rust`
# for more information.
rustc-workspace-hack = "1.0"

# UI test dependencies
clippy_utils = { path = "clippy_utils" }
if_chain = "1.0"
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/guidelines/extern_without_repr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub(super) fn check_item<'tcx>(cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
abi: Abi::C { .. },
items,
} => {
for f_item in items.iter() {
for f_item in *items {
if let Some(Node::ForeignItem(f)) = cx.tcx.hir().find(f_item.id.hir_id()) {
if let ForeignItemKind::Fn(decl, ..) = f.kind {
lint_for_tys(cx, decl.inputs);
Expand Down
12 changes: 9 additions & 3 deletions clippy_lints/src/guidelines/fallible_memory_allocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ use clippy_utils::diagnostics::{span_lint, span_lint_and_note};
use clippy_utils::source::snippet_opt;
use rustc_hir::def::Res;
use rustc_hir::def_id::DefId;
use rustc_hir::intravisit::Visitor;
use rustc_hir::intravisit::{walk_expr, walk_stmt};
use rustc_hir::intravisit::{walk_expr, walk_stmt, Visitor};
use rustc_hir::{Expr, ExprKind, HirId, Local, Node, Path, PathSegment, QPath, Stmt, StmtKind};
use rustc_lint::LateContext;
use rustc_span::symbol::Ident;
Expand Down Expand Up @@ -84,7 +83,14 @@ pub(super) fn check_expr<'tcx>(
let mut ptr_status = PtrStatus::Unverified;
let mut maybe_size_param: Option<&Expr<'_>> = None;

if !cx.tcx.fn_sig(func_did).skip_binder().output().is_unsafe_ptr() {
if !cx
.tcx
.fn_sig(func_did)
.skip_binder()
.output()
.skip_binder()
.is_unsafe_ptr()
{
ptr_status = PtrStatus::NotPtr;
}

Expand Down
41 changes: 21 additions & 20 deletions clippy_lints/src/guidelines/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use clippy_utils::diagnostics::span_lint_and_help;
use clippy_utils::{def_path_def_ids, fn_def_id};
use rustc_data_structures::fx::FxHashSet;
use rustc_hir as hir;
use rustc_hir::def_id::DefIdSet;
use rustc_hir::hir_id::{HirId, HirIdSet};
use rustc_hir::def_id::{DefIdSet, LocalDefId};
use rustc_hir::hir_id::HirIdSet;
use rustc_hir::intravisit;
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_tool_lint, impl_lint_pass};
Expand All @@ -28,13 +28,14 @@ declare_clippy_lint! {
/// which could potentially introduce vulnerablities such as buffer overflow to the software.
///
/// ### Example
/// ```rust
/// ```rust,ignore
/// extern "C" {
/// fn memcpy(dest: *mut c_void, src: *const c_void, n: size_t) -> *mut c_void;
/// }
/// let ptr = unsafe { memcpy(dest, src, size); }
/// // Or use via libc
/// let ptr = unsafe { libc::memcpy(dest, src, size); }
/// ```
#[clippy::version = "1.70.0"]
pub MEM_UNSAFE_FUNCTIONS,
nursery,
Expand Down Expand Up @@ -114,7 +115,7 @@ declare_clippy_lint! {
/// }
/// ```
/// Use instead:
/// ```rust
/// ```rust,ignore
/// use std::time::Duration;
/// pub async fn foo() {
/// tokio::time::sleep(Duration::from_secs(5));
Expand All @@ -137,7 +138,7 @@ declare_clippy_lint! {
/// Possible FP when the user uses proc-macro to generate a function with unsafe block in it.
///
/// ### Example
/// ```rust
/// ```rust,ignore
/// #[proc_macro]
/// pub fn rprintf(input: TokenStream) -> TokenStream {
/// let expr = parse_macro_input!(input as syn::Expr);
Expand All @@ -152,7 +153,7 @@ declare_clippy_lint! {
/// rprintf!();
/// ```
/// Use instead:
/// ```rust
/// ```rust,ignore
/// #[proc_macro]
/// pub fn rprintf(input: TokenStream) -> TokenStream {
/// let expr = parse_macro_input!(input as syn::Expr);
Expand All @@ -177,7 +178,7 @@ declare_clippy_lint! {
/// ### Why is this bad?
///
/// ### Example
/// ```rust
/// ```rust,ignore
/// struct Foo3 {
/// a: libc::c_char,
/// b: libc::c_int,
Expand All @@ -186,7 +187,7 @@ declare_clippy_lint! {
/// extern "C" fn c_abi_fn4(arg_one: u32, arg_two: *const Foo3) {}
/// ```
/// Use instead:
/// ```rust
/// ```rust,ignore
/// #[repr(C)]
/// struct Foo3 {
/// a: libc::c_char,
Expand All @@ -209,11 +210,11 @@ declare_clippy_lint! {
/// This makes code safer, especially in the context of concurrency.
///
/// ### Example
/// ```rust
/// ```rust,ignore
/// let _tm = libc::localtime(&0i64 as *const libc::time_t);
/// ```
/// Use instead:
/// ```rust
/// ```rust,ignore
/// let res = libc::malloc(std::mem::size_of::<libc::tm>());
///
/// libc::locatime_r(&0i64 as *const libc::time_t, res);
Expand All @@ -240,15 +241,15 @@ declare_clippy_lint! {
/// would be assumed non-null even though it wasn't.
///
/// ### Example
/// ```rust
/// let a: *const i8 = std::ptr::null();
/// ```rust,ignore
/// let a: *mut i8 = std::ptr::null_mut();
/// let _ = unsafe { *a };
/// ```
///
/// Use instead:
/// ```rust
/// let a: *const i8 = std::ptr::null();
/// *a = &10_i8;
/// ```rust,ignore
/// let a: *mut i8 = std::ptr::null_mut();
/// unsafe { *a = 10_i8; }
/// let _ = unsafe { *a };
/// ```
#[clippy::version = "1.68.0"]
Expand All @@ -266,15 +267,15 @@ declare_clippy_lint! {
/// it leads program crash or give access to attackers.
///
/// ### Example
/// ```rust
/// ```rust,ignore
/// let ptr: *const u8 = std::ptr::null();
/// unsafe {
/// free(ptr);
/// free(ptr);
/// }
/// ```
/// Use instead:
/// ```rust
/// ```rust,ignore
/// let mut ptr: *const u8 = std::ptr::null();
/// unsafe {
/// free(ptr);
Expand All @@ -301,7 +302,7 @@ declare_clippy_lint! {
/// the pointer is a undefined behavior.
///
/// ### Example
/// ```rust
/// ```rust,ignore
/// unsafe {
/// free(ptr);
/// let val = *ptr;
Expand Down Expand Up @@ -418,7 +419,7 @@ impl<'tcx> LateLintPass<'tcx> for LintGroup {
_decl: &'tcx hir::FnDecl<'_>,
body: &'tcx hir::Body<'_>,
span: Span,
_def_id: HirId,
_def_id: LocalDefId,
) {
if !matches!(kind, intravisit::FnKind::Closure) {
blocking_op_in_async::check_fn(cx, kind, body, span, &self.blocking_fns.ids);
Expand Down Expand Up @@ -531,7 +532,7 @@ fn add_extern_fn_ids(items: &[hir::ForeignItemRef], fns: &mut FnPathsAndIds) {
///
/// i.e.
///
/// ```
/// ```ignore
/// some_expr as *mut i8 as *mut i16 as *mut i32 as *mut i64
/// ```
///
Expand Down
10 changes: 8 additions & 2 deletions clippy_lints/src/guidelines/passing_string_to_c_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@ pub(super) fn check_expr<'tcx>(
if cx.tcx.is_foreign_item(fn_did) {
for param in params {
let str_or_string: Option<&Expr<'_>> = for_each_expr(param, |e| {
let ExprKind::Path(QPath::Resolved(None, Path { res: Res::Local(..), .. })) = e.kind else {
return ControlFlow::Continue(())
let ExprKind::Path(QPath::Resolved(
None,
Path {
res: Res::Local(..), ..
},
)) = e.kind
else {
return ControlFlow::Continue(());
};
let ty = cx.typeck_results().node_type(e.hir_id);
match ty.kind() {
Expand Down
11 changes: 5 additions & 6 deletions clippy_lints/src/guidelines/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ use rustc_span::Span;

use if_chain::if_chain;

use super::peel_casts;
use super::DANGLING_PTR_DEREFERENCE;
use super::NULL_PTR_DEREFERENCE;
use super::PTR_DOUBLE_FREE;
use super::{peel_casts, DANGLING_PTR_DEREFERENCE, NULL_PTR_DEREFERENCE, PTR_DOUBLE_FREE};

macro_rules! span_ptr_lint {
($cx:expr, $lint:ident, $span:expr, $note_span:expr) => {{
Expand Down Expand Up @@ -69,7 +66,9 @@ pub(super) fn check_call(cx: &LateContext<'_>, call_expr: &Expr<'_>, free_fns: &
if !is_lint_allowed(cx, DANGLING_PTR_DEREFERENCE, call_expr.hir_id)
|| !is_lint_allowed(cx, PTR_DOUBLE_FREE, call_expr.hir_id)
{
let Some(mut ptr_validator) = PtrValidator::from_call(cx, call_expr, free_fns) else { return };
let Some(mut ptr_validator) = PtrValidator::from_call(cx, call_expr, free_fns) else {
return;
};

ptr_validator.visit(cx);

Expand Down Expand Up @@ -228,7 +227,7 @@ impl<'a, 'tcx> PtrValidator<'a, 'tcx> {
/// 3. Constant that can be resolved as null pointer.
fn expr_is_creating_null_ptr(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
match expr.kind {
ExprKind::Path(_) if matches!(constant(cx, cx.typeck_results(), expr), Some((Constant::RawPtr(0), _))) => true,
ExprKind::Path(_) if matches!(constant(cx, cx.typeck_results(), expr), Some(Constant::RawPtr(0))) => true,
ExprKind::Cast(inner_expr, cast_ty)
if is_integer_literal(peel_casts(inner_expr), 0) && matches!(cast_ty.kind, TyKind::Ptr(_)) =>
{
Expand Down
7 changes: 2 additions & 5 deletions clippy_lints/src/guidelines/return_stack_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ use rustc_lint::LateContext;
use rustc_span::symbol::sym;
use rustc_span::Span;

use super::peel_casts;
use super::RETURN_STACK_ADDRESS;
use super::{peel_casts, RETURN_STACK_ADDRESS};

pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, block: &'tcx Block<'tcx>, visited_blocks: &mut HirIdSet) {
if visited_blocks.contains(&block.hir_id) {
Expand Down Expand Up @@ -39,10 +38,8 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, block: &'tcx Block<'tcx>, visi
emit_lint(cx, ret.span, decl_span);
}
},
// FIXME: The [`sym::as_mut_ptr`] wasn't available in this toolchain yet,
// change the str comparison to `callee.ident.name == sym::as_mut_ptr` later.
ExprKind::MethodCall(methond_name, caller, ..)
if methond_name.ident.name == sym::as_ptr || methond_name.ident.as_str() == "as_mut_ptr" =>
if methond_name.ident.name == sym::as_ptr || methond_name.ident.name == sym::as_mut_ptr =>
{
let maybe_path = peel_method_calls(caller);
if let Some(decl_span) = get_simple_local_ty_span(cx, maybe_path, &collector.local) {
Expand Down
3 changes: 2 additions & 1 deletion clippy_lints/src/guidelines/unsafe_block_in_proc_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use rustc_data_structures::fx::FxHashSet;
use rustc_hir::{Expr, Item, ItemKind, OwnerNode};
use rustc_lint::LateContext;
use rustc_span::hygiene::{ExpnKind, MacroKind};
use rustc_span::{sym, symbol::Ident, Span};
use rustc_span::symbol::Ident;
use rustc_span::{sym, Span};

// FIXME: Currently, this lint does not have a working ui test,
// because it needs to be tested with `#[proc_macro]` attribute,
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/guidelines_early/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ declare_clippy_lint! {
/// using infinite loops could be as intended.
///
/// ### Example
/// ```rust
/// ```rust,ignore
/// loop {
/// println!("so something");
/// }
/// ```
/// Use instead:
/// ```rust
/// ```rust,ignore
/// loop {
/// println!("do something");
/// if flag {
Expand Down
1 change: 0 additions & 1 deletion clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1121,7 +1121,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
msrv(),
))
});
store.register_late_pass(|_| Box::new(extern_without_repr::ExternWithoutRepr));
let mem_unsafe_fns = conf.mem_unsafe_functions.clone();
let input_fns = conf.input_functions.clone();
let lib_loading_fns = conf.lib_loading_functions.clone();
Expand Down
1 change: 1 addition & 0 deletions tests/ui-toml/guidelines/blocking_op_in_async.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ note: asyncness was determined here
LL | pub async fn async_std_read() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: `-D clippy::blocking-op-in-async` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::blocking_op_in_async)]`

error: this call might blocks the thread in async context
--> $DIR/blocking_op_in_async.rs:17:5
Expand Down
1 change: 1 addition & 0 deletions tests/ui-toml/guidelines/dangling_ptr_dereference.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ note: the pointer was freed here
LL | my_free(ptr);
| ^^^^^^^^^^^^
= note: `-D clippy::dangling-ptr-dereference` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::dangling_ptr_dereference)]`

error: dereferencing a raw pointer that was already freed
--> $DIR/dangling_ptr_dereference.rs:14:26
Expand Down
1 change: 1 addition & 0 deletions tests/ui-toml/guidelines/fallible_memory_allocation.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ note: unverified size used here
LL | let p = my_malloc(size); // lint
| ^^^^
= note: `-D clippy::fallible-memory-allocation` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::fallible_memory_allocation)]`

error: allocating memory without checking if the result pointer is null
--> $DIR/fallible_memory_allocation.rs:38:13
Expand Down
1 change: 1 addition & 0 deletions tests/ui-toml/guidelines/mem_unsafe_functions.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ LL | not_safe();
|
= help: consider using its safe version
= note: `-D clippy::mem-unsafe-functions` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::mem_unsafe_functions)]`

error: use of potentially dangerous memory manipulation function
--> $DIR/mem_unsafe_functions.rs:30:9
Expand Down
1 change: 1 addition & 0 deletions tests/ui-toml/guidelines/non_reentrant_functions.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ LL | let _ = libc::strtok(null_mut(), null()); // lint
|
= help: consider using its reentrant counterpart
= note: `-D clippy::non-reentrant-functions` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::non_reentrant_functions)]`

error: use of non-reentrant function
--> $DIR/non_reentrant_functions.rs:33:9
Expand Down
1 change: 1 addition & 0 deletions tests/ui-toml/guidelines/ptr_double_free.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ note: second free occurred here
LL | my_free(ptr); // lint
| ^^^^^^^^^^^^
= note: `-D clippy::ptr-double-free` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::ptr_double_free)]`

error: pointer was freed multiple times
--> $DIR/ptr_double_free.rs:15:5
Expand Down
1 change: 1 addition & 0 deletions tests/ui-toml/guidelines/untrusted_lib_loading.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ note: untrusted IO function called here
LL | let name_a = untrusted_io();
| ^^^^^^^^^^^^^^
= note: `-D clippy::untrusted-lib-loading` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::untrusted_lib_loading)]`

error: loading dynamic library from untrusted source
--> $DIR/untrusted_lib_loading.rs:36:18
Expand Down
Loading

0 comments on commit f25fa70

Please sign in to comment.