Skip to content

Commit

Permalink
api adjusting & dogfood tests etc
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 5c3fcf2
Show file tree
Hide file tree
Showing 34 changed files with 69 additions and 34 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
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
6 changes: 3 additions & 3 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 Down Expand Up @@ -418,7 +418,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
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
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
15 changes: 11 additions & 4 deletions tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ error: error reading Clippy's configuration file: unknown field `foobar`, expect
absolute-paths-max-segments
accept-comment-above-attributes
accept-comment-above-statement
alloc-size-check-functions
allow-dbg-in-tests
allow-expect-in-tests
allow-io-blocking-ops
Expand Down Expand Up @@ -48,19 +47,19 @@ error: error reading Clippy's configuration file: unknown field `foobar`, expect
max-struct-bools
max-suggested-slice-pattern-length
max-trait-bounds
min-ident-chars-threshold
missing-docs-in-crate-items
mem-alloc-functions
mem-free-functions
mem-unsafe-functions
min-ident-chars-threshold
missing-docs-in-crate-items
msrv
non-reentrant-functions
pass-by-value-size-limit
semicolon-inside-block-ignore-singleline
semicolon-outside-block-ignore-multiline
single-char-binding-names-threshold
stack-size-threshold
size-checking-function-keywords
stack-size-threshold
standard-macro-braces
suppress-restriction-lint-in-const
third-party
Expand All @@ -87,6 +86,7 @@ error: error reading Clippy's configuration file: unknown field `barfoo`, expect
accept-comment-above-statement
allow-dbg-in-tests
allow-expect-in-tests
allow-io-blocking-ops
allow-mixed-uninlined-format-args
allow-one-hash-in-raw-strings
allow-print-in-tests
Expand Down Expand Up @@ -118,21 +118,28 @@ error: error reading Clippy's configuration file: unknown field `barfoo`, expect
excessive-nesting-threshold
future-size-threshold
ignore-interior-mutability
input-functions
large-error-threshold
lib-loading-functions
literal-representation-threshold
matches-for-let-else
max-fn-params-bools
max-include-file-size
max-struct-bools
max-suggested-slice-pattern-length
max-trait-bounds
mem-alloc-functions
mem-free-functions
mem-unsafe-functions
min-ident-chars-threshold
missing-docs-in-crate-items
msrv
non-reentrant-functions
pass-by-value-size-limit
semicolon-inside-block-ignore-singleline
semicolon-outside-block-ignore-multiline
single-char-binding-names-threshold
size-checking-function-keywords
stack-size-threshold
standard-macro-braces
suppress-restriction-lint-in-const
Expand Down
1 change: 1 addition & 0 deletions tests/ui/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_sleep() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= 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:39:9
Expand Down
1 change: 1 addition & 0 deletions tests/ui/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 | 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:24:22
Expand Down
1 change: 1 addition & 0 deletions tests/ui/guidelines/extern_without_repr.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ note: type declared here
LL | struct ReprSimd {
| ^^^^^^^^^^^^^^^
= note: `-D clippy::extern-without-repr` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::extern_without_repr)]`

error: should use `#[repr(..)]` to specifing data layout when type is used in FFI
--> $DIR/extern_without_repr.rs:63:45
Expand Down
1 change: 1 addition & 0 deletions tests/ui/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 = 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:26:13
Expand Down
1 change: 1 addition & 0 deletions tests/ui/guidelines/implicit_abi.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ LL | extern {
| ^^^^^^ help: explicitly states ABI instead: `extern "C"`
|
= note: `-D clippy::implicit-abi` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::implicit_abi)]`

error: missing ABI label on extern block
--> $DIR/implicit_abi.rs:23:1
Expand Down
4 changes: 1 addition & 3 deletions tests/ui/guidelines/infinite_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,7 @@ fn match_pat() {
} // don't lint

loop {
let Ok(val) = result.map(|v| 10) else {
break
}; // don't lint
let Ok(val) = result.map(|v| 10) else { break }; // don't lint
}
}

Expand Down
1 change: 1 addition & 0 deletions tests/ui/guidelines/infinite_loop.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ LL | | } // lint
|
= help: consider adding `break` or `return` statement in the loop block
= note: `-D clippy::infinite-loop` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::infinite_loop)]`

error: loop without break condition
--> $DIR/infinite_loop.rs:42:5
Expand Down
6 changes: 2 additions & 4 deletions tests/ui/guidelines/mem_unsafe_functions.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
#![feature(c_size_t)]
#![warn(clippy::mem_unsafe_functions)]

use core::ffi::c_size_t as size_t;
use core::ffi::{c_char, c_int, c_size_t, c_void};
use core::ffi::{c_char, c_int, c_size_t as size_t, c_size_t, c_void};
use std::ptr::{null, null_mut};

// mock libc crate
mod libc {
pub use core::ffi::c_size_t as size_t;
pub use core::ffi::{c_char, c_int, c_void};
pub use core::ffi::{c_char, c_int, c_size_t as size_t, c_void};
extern "C" {
pub fn strcpy(dst: *mut c_char, src: *const c_char) -> *mut c_char;
pub fn strncpy(dst: *mut c_char, src: *const c_char, n: size_t) -> *mut c_char;
Expand Down
1 change: 1 addition & 0 deletions tests/ui/guidelines/mem_unsafe_functions.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ LL | let _ = memcpy(dst, src, n);
|
= 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:36:13
Expand Down
1 change: 1 addition & 0 deletions tests/ui/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(string, delim); // 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:38:21
Expand Down
1 change: 1 addition & 0 deletions tests/ui/guidelines/null_ptr_dereference.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ note: first dereference occurred here
LL | do_something((*a).inner);
| ^^^^
= note: `-D clippy::null-ptr-dereference` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::null_ptr_dereference)]`

error: dereferencing null pointer
--> $DIR/null_ptr_dereference.rs:29:5
Expand Down
1 change: 1 addition & 0 deletions tests/ui/guidelines/passing_string_to_c_functions.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ help: use `CString` or `CStr` instead
LL | greet(name.as_ptr() as *const _); // lint
| ^^^^
= note: `-D clippy::passing-string-to-c-functions` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::passing_string_to_c_functions)]`

error: passing native strings to external functions
--> $DIR/passing_string_to_c_functions.rs:26:9
Expand Down
Loading

0 comments on commit 5c3fcf2

Please sign in to comment.