Skip to content

Commit

Permalink
Merge pull request #31 from TogetherGame/custom_lints_1.74.0
Browse files Browse the repository at this point in the history
Migrate customizations from `rust-1.68.0` to `rust-1.74.0`
  • Loading branch information
surechen authored Dec 1, 2023
2 parents 7671c28 + f25fa70 commit d5b213b
Show file tree
Hide file tree
Showing 66 changed files with 5,500 additions and 57 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4797,6 +4797,7 @@ Released 2018-09-13
[`blanket_clippy_restriction_lints`]: https://rust-lang.github.io/rust-clippy/master/index.html#blanket_clippy_restriction_lints
[`block_in_if_condition_expr`]: https://rust-lang.github.io/rust-clippy/master/index.html#block_in_if_condition_expr
[`block_in_if_condition_stmt`]: https://rust-lang.github.io/rust-clippy/master/index.html#block_in_if_condition_stmt
[`blocking_op_in_async`]: https://rust-lang.github.io/rust-clippy/master/index.html#blocking_op_in_async
[`blocks_in_if_conditions`]: https://rust-lang.github.io/rust-clippy/master/index.html#blocks_in_if_conditions
[`bool_assert_comparison`]: https://rust-lang.github.io/rust-clippy/master/index.html#bool_assert_comparison
[`bool_comparison`]: https://rust-lang.github.io/rust-clippy/master/index.html#bool_comparison
Expand Down Expand Up @@ -4854,6 +4855,7 @@ Released 2018-09-13
[`create_dir`]: https://rust-lang.github.io/rust-clippy/master/index.html#create_dir
[`crosspointer_transmute`]: https://rust-lang.github.io/rust-clippy/master/index.html#crosspointer_transmute
[`cyclomatic_complexity`]: https://rust-lang.github.io/rust-clippy/master/index.html#cyclomatic_complexity
[`dangling_ptr_dereference`]: https://rust-lang.github.io/rust-clippy/master/index.html#dangling_ptr_dereference
[`dbg_macro`]: https://rust-lang.github.io/rust-clippy/master/index.html#dbg_macro
[`debug_assert_with_mut_call`]: https://rust-lang.github.io/rust-clippy/master/index.html#debug_assert_with_mut_call
[`decimal_literal_representation`]: https://rust-lang.github.io/rust-clippy/master/index.html#decimal_literal_representation
Expand Down Expand Up @@ -4926,9 +4928,11 @@ Released 2018-09-13
[`explicit_write`]: https://rust-lang.github.io/rust-clippy/master/index.html#explicit_write
[`extend_from_slice`]: https://rust-lang.github.io/rust-clippy/master/index.html#extend_from_slice
[`extend_with_drain`]: https://rust-lang.github.io/rust-clippy/master/index.html#extend_with_drain
[`extern_without_repr`]: https://rust-lang.github.io/rust-clippy/master/index.html#extern_without_repr
[`extra_unused_lifetimes`]: https://rust-lang.github.io/rust-clippy/master/index.html#extra_unused_lifetimes
[`extra_unused_type_parameters`]: https://rust-lang.github.io/rust-clippy/master/index.html#extra_unused_type_parameters
[`fallible_impl_from`]: https://rust-lang.github.io/rust-clippy/master/index.html#fallible_impl_from
[`fallible_memory_allocation`]: https://rust-lang.github.io/rust-clippy/master/index.html#fallible_memory_allocation
[`field_reassign_with_default`]: https://rust-lang.github.io/rust-clippy/master/index.html#field_reassign_with_default
[`filetype_is_file`]: https://rust-lang.github.io/rust-clippy/master/index.html#filetype_is_file
[`filter_map`]: https://rust-lang.github.io/rust-clippy/master/index.html#filter_map
Expand Down Expand Up @@ -4980,6 +4984,7 @@ Released 2018-09-13
[`ifs_same_cond`]: https://rust-lang.github.io/rust-clippy/master/index.html#ifs_same_cond
[`ignored_unit_patterns`]: https://rust-lang.github.io/rust-clippy/master/index.html#ignored_unit_patterns
[`impl_trait_in_params`]: https://rust-lang.github.io/rust-clippy/master/index.html#impl_trait_in_params
[`implicit_abi`]: https://rust-lang.github.io/rust-clippy/master/index.html#implicit_abi
[`implicit_clone`]: https://rust-lang.github.io/rust-clippy/master/index.html#implicit_clone
[`implicit_hasher`]: https://rust-lang.github.io/rust-clippy/master/index.html#implicit_hasher
[`implicit_return`]: https://rust-lang.github.io/rust-clippy/master/index.html#implicit_return
Expand All @@ -4998,6 +5003,7 @@ Released 2018-09-13
[`inefficient_to_string`]: https://rust-lang.github.io/rust-clippy/master/index.html#inefficient_to_string
[`infallible_destructuring_match`]: https://rust-lang.github.io/rust-clippy/master/index.html#infallible_destructuring_match
[`infinite_iter`]: https://rust-lang.github.io/rust-clippy/master/index.html#infinite_iter
[`infinite_loop`]: https://rust-lang.github.io/rust-clippy/master/index.html#infinite_loop
[`inherent_to_string`]: https://rust-lang.github.io/rust-clippy/master/index.html#inherent_to_string
[`inherent_to_string_shadow_display`]: https://rust-lang.github.io/rust-clippy/master/index.html#inherent_to_string_shadow_display
[`init_numbered_fields`]: https://rust-lang.github.io/rust-clippy/master/index.html#init_numbered_fields
Expand Down Expand Up @@ -5060,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 @@ -5124,6 +5131,7 @@ 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
[`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
Expand Down Expand Up @@ -5211,11 +5219,13 @@ Released 2018-09-13
[`non_canonical_partial_ord_impl`]: https://rust-lang.github.io/rust-clippy/master/index.html#non_canonical_partial_ord_impl
[`non_minimal_cfg`]: https://rust-lang.github.io/rust-clippy/master/index.html#non_minimal_cfg
[`non_octal_unix_permissions`]: https://rust-lang.github.io/rust-clippy/master/index.html#non_octal_unix_permissions
[`non_reentrant_functions`]: https://rust-lang.github.io/rust-clippy/master/index.html#non_reentrant_functions
[`non_send_fields_in_send_ty`]: https://rust-lang.github.io/rust-clippy/master/index.html#non_send_fields_in_send_ty
[`nonminimal_bool`]: https://rust-lang.github.io/rust-clippy/master/index.html#nonminimal_bool
[`nonsensical_open_options`]: https://rust-lang.github.io/rust-clippy/master/index.html#nonsensical_open_options
[`nonstandard_macro_braces`]: https://rust-lang.github.io/rust-clippy/master/index.html#nonstandard_macro_braces
[`not_unsafe_ptr_arg_deref`]: https://rust-lang.github.io/rust-clippy/master/index.html#not_unsafe_ptr_arg_deref
[`null_ptr_dereference`]: https://rust-lang.github.io/rust-clippy/master/index.html#null_ptr_dereference
[`obfuscated_if_else`]: https://rust-lang.github.io/rust-clippy/master/index.html#obfuscated_if_else
[`octal_escapes`]: https://rust-lang.github.io/rust-clippy/master/index.html#octal_escapes
[`ok_expect`]: https://rust-lang.github.io/rust-clippy/master/index.html#ok_expect
Expand Down Expand Up @@ -5245,6 +5255,7 @@ Released 2018-09-13
[`partial_pub_fields`]: https://rust-lang.github.io/rust-clippy/master/index.html#partial_pub_fields
[`partialeq_ne_impl`]: https://rust-lang.github.io/rust-clippy/master/index.html#partialeq_ne_impl
[`partialeq_to_none`]: https://rust-lang.github.io/rust-clippy/master/index.html#partialeq_to_none
[`passing_string_to_c_functions`]: https://rust-lang.github.io/rust-clippy/master/index.html#passing_string_to_c_functions
[`path_buf_push_overwrite`]: https://rust-lang.github.io/rust-clippy/master/index.html#path_buf_push_overwrite
[`path_ends_with_ext`]: https://rust-lang.github.io/rust-clippy/master/index.html#path_ends_with_ext
[`pattern_type_mismatch`]: https://rust-lang.github.io/rust-clippy/master/index.html#pattern_type_mismatch
Expand All @@ -5261,6 +5272,7 @@ Released 2018-09-13
[`ptr_arg`]: https://rust-lang.github.io/rust-clippy/master/index.html#ptr_arg
[`ptr_as_ptr`]: https://rust-lang.github.io/rust-clippy/master/index.html#ptr_as_ptr
[`ptr_cast_constness`]: https://rust-lang.github.io/rust-clippy/master/index.html#ptr_cast_constness
[`ptr_double_free`]: https://rust-lang.github.io/rust-clippy/master/index.html#ptr_double_free
[`ptr_eq`]: https://rust-lang.github.io/rust-clippy/master/index.html#ptr_eq
[`ptr_offset_with_cast`]: https://rust-lang.github.io/rust-clippy/master/index.html#ptr_offset_with_cast
[`pub_enum_variant_names`]: https://rust-lang.github.io/rust-clippy/master/index.html#pub_enum_variant_names
Expand Down Expand Up @@ -5317,6 +5329,7 @@ Released 2018-09-13
[`result_unit_err`]: https://rust-lang.github.io/rust-clippy/master/index.html#result_unit_err
[`result_unwrap_used`]: https://rust-lang.github.io/rust-clippy/master/index.html#result_unwrap_used
[`return_self_not_must_use`]: https://rust-lang.github.io/rust-clippy/master/index.html#return_self_not_must_use
[`return_stack_address`]: https://rust-lang.github.io/rust-clippy/master/index.html#return_stack_address
[`reversed_empty_ranges`]: https://rust-lang.github.io/rust-clippy/master/index.html#reversed_empty_ranges
[`same_functions_in_if_condition`]: https://rust-lang.github.io/rust-clippy/master/index.html#same_functions_in_if_condition
[`same_item_push`]: https://rust-lang.github.io/rust-clippy/master/index.html#same_item_push
Expand Down Expand Up @@ -5457,13 +5470,15 @@ Released 2018-09-13
[`unnested_or_patterns`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnested_or_patterns
[`unreachable`]: https://rust-lang.github.io/rust-clippy/master/index.html#unreachable
[`unreadable_literal`]: https://rust-lang.github.io/rust-clippy/master/index.html#unreadable_literal
[`unsafe_block_in_proc_macro`]: https://rust-lang.github.io/rust-clippy/master/index.html#unsafe_block_in_proc_macro
[`unsafe_derive_deserialize`]: https://rust-lang.github.io/rust-clippy/master/index.html#unsafe_derive_deserialize
[`unsafe_removed_from_name`]: https://rust-lang.github.io/rust-clippy/master/index.html#unsafe_removed_from_name
[`unsafe_vector_initialization`]: https://rust-lang.github.io/rust-clippy/master/index.html#unsafe_vector_initialization
[`unseparated_literal_suffix`]: https://rust-lang.github.io/rust-clippy/master/index.html#unseparated_literal_suffix
[`unsound_collection_transmute`]: https://rust-lang.github.io/rust-clippy/master/index.html#unsound_collection_transmute
[`unstable_as_mut_slice`]: https://rust-lang.github.io/rust-clippy/master/index.html#unstable_as_mut_slice
[`unstable_as_slice`]: https://rust-lang.github.io/rust-clippy/master/index.html#unstable_as_slice
[`untrusted_lib_loading`]: https://rust-lang.github.io/rust-clippy/master/index.html#untrusted_lib_loading
[`unused_async`]: https://rust-lang.github.io/rust-clippy/master/index.html#unused_async
[`unused_collect`]: https://rust-lang.github.io/rust-clippy/master/index.html#unused_collect
[`unused_format_specs`]: https://rust-lang.github.io/rust-clippy/master/index.html#unused_format_specs
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ walkdir = "2.3"
# This is used by the `collect-metadata` alias.
filetime = "0.2"
itertools = "0.10.1"
libloading = "0.8.0"

# UI test dependencies
clippy_utils = { path = "clippy_utils" }
Expand Down
14 changes: 14 additions & 0 deletions clippy_lints/src/declared_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,20 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
crate::functions::TOO_MANY_ARGUMENTS_INFO,
crate::functions::TOO_MANY_LINES_INFO,
crate::future_not_send::FUTURE_NOT_SEND_INFO,
crate::guidelines::BLOCKING_OP_IN_ASYNC_INFO,
crate::guidelines::DANGLING_PTR_DEREFERENCE_INFO,
crate::guidelines::EXTERN_WITHOUT_REPR_INFO,
crate::guidelines::FALLIBLE_MEMORY_ALLOCATION_INFO,
crate::guidelines::MEM_UNSAFE_FUNCTIONS_INFO,
crate::guidelines::NON_REENTRANT_FUNCTIONS_INFO,
crate::guidelines::NULL_PTR_DEREFERENCE_INFO,
crate::guidelines::PASSING_STRING_TO_C_FUNCTIONS_INFO,
crate::guidelines::PTR_DOUBLE_FREE_INFO,
crate::guidelines::RETURN_STACK_ADDRESS_INFO,
crate::guidelines::UNSAFE_BLOCK_IN_PROC_MACRO_INFO,
crate::guidelines::UNTRUSTED_LIB_LOADING_INFO,
crate::guidelines_early::IMPLICIT_ABI_INFO,
crate::guidelines_early::INFINITE_LOOP_INFO,
crate::if_let_mutex::IF_LET_MUTEX_INFO,
crate::if_not_else::IF_NOT_ELSE_INFO,
crate::if_then_some_else_none::IF_THEN_SOME_ELSE_NONE_INFO,
Expand Down
123 changes: 123 additions & 0 deletions clippy_lints/src/guidelines/blocking_op_in_async.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
use std::ops::ControlFlow;

use clippy_utils::diagnostics::span_lint_and_note;
use clippy_utils::source::snippet_opt;
use clippy_utils::visitors::{for_each_expr_with_closures, Visitable};
use clippy_utils::{def_path_def_ids, fn_def_id, is_async_fn};
use rustc_hir::def_id::DefIdSet;
use rustc_hir::intravisit::FnKind;
use rustc_hir::{Body, Closure, Expr, ExprKind, Node};
use rustc_lint::LateContext;
use rustc_span::Span;

use super::BLOCKING_OP_IN_ASYNC;

/// Basic list of functions' path to check for
static FUNCTIONS_BLACKLIST: &[&[&str]] = &[&["std", "thread", "sleep"]];
/// Functions in these slice will be checked if `allow-io-blocking-ops` option
/// was set to `false` in user configuration.
static IO_FUNCTIONS_BLACKLIST: &[&[&str]] = &[
&["std", "fs", "try_exists"],
&["std", "fs", "canonicalize"],
&["std", "fs", "copy"],
&["std", "fs", "create_dir"],
&["std", "fs", "create_dir_all"],
&["std", "fs", "hard_link"],
&["std", "fs", "metadata"],
&["std", "fs", "read"],
&["std", "fs", "read_dir"],
&["std", "fs", "read_link"],
&["std", "fs", "read_to_string"],
&["std", "fs", "remove_dir"],
&["std", "fs", "remove_dir_all"],
&["std", "fs", "remove_file"],
&["std", "fs", "rename"],
&["std", "fs", "set_permissions"],
&["std", "fs", "symlink_metadata"],
&["std", "fs", "write"],
&["std", "io", "copy"],
&["std", "io", "empty"],
&["std", "io", "read_to_string"],
&["std", "io", "repeat"],
&["std", "io", "sink"],
&["std", "io", "stderr"],
&["std", "io", "stdin"],
&["std", "io", "stdout"],
];

pub(super) fn init_blacklist_ids(cx: &LateContext<'_>, allow_io_blocking_ops: bool, blacklist_ids: &mut DefIdSet) {
let mut insert_did = |list: &[&[&str]]| {
for fn_path in list {
for did in def_path_def_ids(cx, fn_path) {
blacklist_ids.insert(did);
}
}
};

insert_did(FUNCTIONS_BLACKLIST);
if !allow_io_blocking_ops {
insert_did(IO_FUNCTIONS_BLACKLIST);
}
}

pub(super) fn check_fn<'tcx>(
cx: &LateContext<'tcx>,
kind: FnKind<'_>,
body: &'tcx Body<'_>,
span: Span,
blacklist_ids: &DefIdSet,
) {
if !is_async_fn(kind) {
return;
}
let decl_span = cx.tcx.sess.source_map().guess_head_span(span);
lint_blacklisted_call(cx, body, blacklist_ids, decl_span);
}

pub(super) fn check_expr<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, blacklist_ids: &DefIdSet) {
if let ExprKind::Closure(Closure { body, .. }) = expr.kind &&
let Some(body_node) = cx.tcx.hir().find(body.hir_id) &&
let Node::Expr(body_expr) = body_node &&
let Some(async_span) = get_async_span(cx, expr.span)
{
lint_blacklisted_call(cx, body_expr, blacklist_ids, async_span);
}
}

/// Return the `async` keyword span for a closure if it starts with one.
fn get_async_span(cx: &LateContext<'_>, span: Span) -> Option<Span> {
let start_span = cx.tcx.sess.source_map().span_until_whitespace(span);
if snippet_opt(cx, start_span)
.filter(|snippet| snippet == "async")
.is_some()
{
Some(start_span)
} else {
None
}
}

fn lint_blacklisted_call<'tcx>(
cx: &LateContext<'tcx>,
node: impl Visitable<'tcx>,
blacklist_ids: &DefIdSet,
note_span: Span,
) {
let mut blocking_call_spans = vec![];
for_each_expr_with_closures(cx, node, |e| {
if let Some(did) = fn_def_id(cx, e) && blacklist_ids.contains(&did) {
blocking_call_spans.push(e.span);
}
ControlFlow::<()>::Continue(())
});
for call_span in blocking_call_spans {
span_lint_and_note(
cx,
BLOCKING_OP_IN_ASYNC,
call_span,
"this call might blocks the thread in async context",
Some(note_span),
"asyncness was determined here",
);
}
}
67 changes: 67 additions & 0 deletions clippy_lints/src/guidelines/extern_without_repr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use super::EXTERN_WITHOUT_REPR;
use clippy_utils::diagnostics::span_lint_and_note;
use clippy_utils::ty::walk_ptrs_hir_ty;
use rustc_hir::{FnHeader, FnSig, ForeignItemKind, Item, ItemKind, Node, Ty};
use rustc_hir_analysis::hir_ty_to_ty;
use rustc_lint::LateContext;
use rustc_span::Span;
use rustc_target::spec::abi::Abi;

pub(super) fn check_item<'tcx>(cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
match &item.kind {
ItemKind::Fn(
FnSig {
header: FnHeader { abi: Abi::C { .. }, .. },
decl,
..
},
..,
) => lint_for_tys(cx, decl.inputs),
ItemKind::ForeignMod {
abi: Abi::C { .. },
items,
} => {
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);
}
}
}
},
_ => (),
}
}

/// Return the span of where the given `ty` was declared if it DOES NOT
/// have `repr(C|transparent|packed|align(x))` attribute.
fn non_ffi_safe_ty_span(cx: &LateContext<'_>, ty: &Ty<'_>) -> Option<Span> {
let mid_ty = hir_ty_to_ty(cx.tcx, walk_ptrs_hir_ty(ty));
let adt = mid_ty.ty_adt_def()?;
if !adt.did().is_local() || adt.variants().is_empty() {
return None;
}
let repr = adt.repr();
if repr.inhibit_enum_layout_opt() || repr.transparent() || repr.packed() || repr.align.is_some() {
None
} else {
Some(cx.tcx.def_span(adt.did()))
}
}

fn lint_for_tys(cx: &LateContext<'_>, tys: &[Ty<'_>]) {
let decl_and_usage_spans: Vec<(Span, Span)> = tys
.iter()
.filter_map(|ty| non_ffi_safe_ty_span(cx, ty).map(|s| (s, ty.span)))
.collect();
for (decl_span, usage_span) in decl_and_usage_spans {
span_lint_and_note(
cx,
EXTERN_WITHOUT_REPR,
usage_span,
"should use `#[repr(..)]` to specifing data layout when type is used in FFI",
Some(decl_span),
"type declared here",
);
}
}
Loading

0 comments on commit d5b213b

Please sign in to comment.