Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit 3a413b7
Merge: d5a1eff 4d8af6f
Author: surechen <chenshuo17@huawei.com>
Date:   Mon Jun 26 11:32:14 2023 +0800

    Merge pull request rust-lang#5 from mojave2/lint_loop_exit

    new_lint loop_without_break_or_return

commit 4d8af6f
Author: mojave2 <0109chenchen@gmail.com>
Date:   Sun Jun 25 19:21:11 2023 +0800

    new_line loop_without_break_or_return

    Signed-off-by: mojave2 <0109chenchen@gmail.com>

commit d5a1eff
Merge: af6c9d4 9cd4b13
Author: surechen <chenshuo17@huawei.com>
Date:   Mon Jun 26 11:23:44 2023 +0800

    Merge pull request rust-lang#2 from mojave2/dev_stable

    new_lint non_reentrant_functions

commit 9cd4b13
Author: mojave2 <0109chenchen@gmail.com>
Date:   Wed Jun 21 17:44:12 2023 +0800

    update_lint non_reentrant_functions

    Signed-off-by: mojave2 <0109chenchen@gmail.com>

commit c900d4a
Author: mojave2 <0109chenchen@gmail.com>
Date:   Mon Jun 19 18:37:16 2023 +0800

    update_lint non_reentrant_functions

    Signed-off-by: mojave2 <0109chenchen@gmail.com>

commit 9845b3a
Author: mojave2 <0109chenchen@gmail.com>
Date:   Sat Jun 17 16:32:10 2023 +0800

    improve implementation by dogfood

    Signed-off-by: mojave2 <0109chenchen@gmail.com>

commit a3a1415
Author: mojave2 <0109chenchen@gmail.com>
Date:   Sat Jun 17 15:29:11 2023 +0800

    doc-comment non_reentrant_functions

    Signed-off-by: mojave2 <0109chenchen@gmail.com>

commit eb80bb0
Author: mojave2 <0109chenchen@gmail.com>
Date:   Sat Jun 17 15:14:34 2023 +0800

    new_lint non_reentrant_functions

    Signed-off-by: mojave2 <0109chenchen@gmail.com>

commit af6c9d4
Merge: 38ab3a3 33ddd19
Author: surechen <chenshuo17@huawei.com>
Date:   Mon Jun 26 10:54:08 2023 +0800

    Merge pull request rust-lang#7 from J-ZhengLi/dev_stable

    fix test fail after dev fmt of [`implicit_abi`]

commit 33ddd19
Author: J-ZhengLi <lizheng135@huawei.com>
Date:   Mon Jun 26 10:15:26 2023 +0800

    fix test fail after dev fmt of [`implicit_abi`]

    Signed-off-by: J-ZhengLi <lizheng135@huawei.com>

commit 38ab3a3
Merge: 83e42a2 5ce5313
Author: surechen <chenshuo17@huawei.com>
Date:   Mon Jun 26 09:47:22 2023 +0800

    Merge pull request rust-lang#1 from J-ZhengLi/dev_stable

    add new lints: [`mem_unsafe_functions`], [`unsafe_block_in_proc_macro`] and [`implicit_abi`]

commit 5ce5313
Author: J-ZhengLi <lizheng135@huawei.com>
Date:   Wed Jun 21 11:24:29 2023 +0800

    add new lint [`implicit_abi`]
    that detects external block without explicit label "C"

    Signed-off-by: J-ZhengLi <lizheng135@huawei.com>

commit cc5ab4a
Author: J-ZhengLi <lizheng135@huawei.com>
Date:   Tue Jun 20 12:44:02 2023 +0800

    add new lint [`unsafe_block_in_proc_macro`]
    that detects unsafe block in `quote!`

    Signed-off-by: J-ZhengLi <lizheng135@huawei.com>

commit c091b77
Author: J-ZhengLi <lizheng135@huawei.com>
Date:   Sat Jun 17 15:59:56 2023 +0800

    add configuration for lint [`mem_unsafe_functions`];
    finish implement lint [`mem_unsafe_functions`];
    add simple description for unimplemented lints;

    Signed-off-by: J-ZhengLi <lizheng135@huawei.com>

commit a50f308
Author: J-ZhengLi <lizheng135@huawei.com>
Date:   Fri Jun 16 17:45:56 2023 +0800

    init following new lints:

    [`passing_string_to_c_functions`]
    [`untrusted_lib_loading`]
    [`falliable_memory_allocation`]

    Signed-off-by: J-ZhengLi <lizheng135@huawei.com>

commit da3cc15
Author: J-ZhengLi <lizheng135@huawei.com>
Date:   Fri Jun 16 14:11:59 2023 +0800

    add `guidelines` type and barebone for [`mem_unsafe_functions`] lint

    Signed-off-by: J-ZhengLi <lizheng135@huawei.com>

# Conflicts:
#	clippy_lints/src/lib.rs
  • Loading branch information
surechen committed Jun 26, 2023
1 parent 8b7e60e commit 4f6422c
Show file tree
Hide file tree
Showing 31 changed files with 1,448 additions and 0 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4532,6 +4532,7 @@ Released 2018-09-13
[`extend_with_drain`]: https://rust-lang.github.io/rust-clippy/master/index.html#extend_with_drain
[`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
[`falliable_memory_allocation`]: https://rust-lang.github.io/rust-clippy/master/index.html#falliable_memory_allocation
[`fallible_impl_from`]: https://rust-lang.github.io/rust-clippy/master/index.html#fallible_impl_from
[`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
Expand Down Expand Up @@ -4579,6 +4580,7 @@ Released 2018-09-13
[`if_then_some_else_none`]: https://rust-lang.github.io/rust-clippy/master/index.html#if_then_some_else_none
[`ifs_same_cond`]: https://rust-lang.github.io/rust-clippy/master/index.html#ifs_same_cond
[`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 Down Expand Up @@ -4650,6 +4652,7 @@ Released 2018-09-13
[`lines_filter_map_ok`]: https://rust-lang.github.io/rust-clippy/master/index.html#lines_filter_map_ok
[`linkedlist`]: https://rust-lang.github.io/rust-clippy/master/index.html#linkedlist
[`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 @@ -4707,6 +4710,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_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 Expand Up @@ -4779,6 +4783,7 @@ Released 2018-09-13
[`no_mangle_with_rust_abi`]: https://rust-lang.github.io/rust-clippy/master/index.html#no_mangle_with_rust_abi
[`non_ascii_literal`]: https://rust-lang.github.io/rust-clippy/master/index.html#non_ascii_literal
[`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
Expand Down Expand Up @@ -4813,6 +4818,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
[`pattern_type_mismatch`]: https://rust-lang.github.io/rust-clippy/master/index.html#pattern_type_mismatch
[`permissions_set_readonly_false`]: https://rust-lang.github.io/rust-clippy/master/index.html#permissions_set_readonly_false
Expand Down Expand Up @@ -5003,13 +5009,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
8 changes: 8 additions & 0 deletions clippy_lints/src/declared_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,14 @@ 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::FALLIABLE_MEMORY_ALLOCATION_INFO,
crate::guidelines::MEM_UNSAFE_FUNCTIONS_INFO,
crate::guidelines::PASSING_STRING_TO_C_FUNCTIONS_INFO,
crate::guidelines::UNTRUSTED_LIB_LOADING_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,
crate::implicit_abi::IMPLICIT_ABI_INFO,
crate::implicit_hasher::IMPLICIT_HASHER_INFO,
crate::implicit_return::IMPLICIT_RETURN_INFO,
crate::implicit_saturating_add::IMPLICIT_SATURATING_ADD_INFO,
Expand Down Expand Up @@ -240,6 +245,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
crate::literal_representation::MISTYPED_LITERAL_SUFFIXES_INFO,
crate::literal_representation::UNREADABLE_LITERAL_INFO,
crate::literal_representation::UNUSUAL_BYTE_GROUPINGS_INFO,
crate::loop_without_break_or_return::LOOP_WITHOUT_BREAK_OR_RETURN_INFO,
crate::loops::EMPTY_LOOP_INFO,
crate::loops::EXPLICIT_COUNTER_LOOP_INFO,
crate::loops::EXPLICIT_INTO_ITER_LOOP_INFO,
Expand Down Expand Up @@ -467,6 +473,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
crate::non_expressive_names::MANY_SINGLE_CHAR_NAMES_INFO,
crate::non_expressive_names::SIMILAR_NAMES_INFO,
crate::non_octal_unix_permissions::NON_OCTAL_UNIX_PERMISSIONS_INFO,
crate::non_reentrant_functions::NON_REENTRANT_FUNCTIONS_INFO,
crate::non_send_fields_in_send_ty::NON_SEND_FIELDS_IN_SEND_TY_INFO,
crate::nonstandard_macro_braces::NONSTANDARD_MACRO_BRACES_INFO,
crate::octal_escapes::OCTAL_ESCAPES_INFO,
Expand Down Expand Up @@ -629,6 +636,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
crate::unnecessary_struct_initialization::UNNECESSARY_STRUCT_INITIALIZATION_INFO,
crate::unnecessary_wraps::UNNECESSARY_WRAPS_INFO,
crate::unnested_or_patterns::UNNESTED_OR_PATTERNS_INFO,
crate::unsafe_block_in_proc_macro::UNSAFE_BLOCK_IN_PROC_MACRO_INFO,
crate::unsafe_removed_from_name::UNSAFE_REMOVED_FROM_NAME_INFO,
crate::unused_async::UNUSED_ASYNC_INFO,
crate::unused_io_amount::UNUSED_IO_AMOUNT_INFO,
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/guidelines/falliable_memory_allocation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

45 changes: 45 additions & 0 deletions clippy_lints/src/guidelines/mem_unsafe_functions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use clippy_utils::diagnostics::span_lint_and_help;
use if_chain::if_chain;
use rustc_hir::def::Res;
use rustc_hir::def_id::DefIdSet;
use rustc_hir::{Expr, ExprKind, Item, ItemKind, QPath};
use rustc_lint::LateContext;

use super::MEM_UNSAFE_FUNCTIONS;

/// Check extern function definitions.
///
/// The main purpose of this function is to load `def_ids` of declared external functions.
pub(super) fn check_foreign_item(item: &Item<'_>, blacklist: &[String], blacklist_ids: &mut DefIdSet) {
if let ItemKind::ForeignMod { items, .. } = item.kind {
for f_item in items {
if blacklist.contains(&f_item.ident.as_str().to_string()) {
let f_did = f_item.id.hir_id().owner.def_id.to_def_id();
blacklist_ids.insert(f_did);
}
}
}
}

/// Check function call expression
///
/// Will lint if the name of called function was blacklisted by the configuration.
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, blacklist_ids: &DefIdSet) {
if_chain! {
if let ExprKind::Call(fn_expr, _) = &expr.kind;
if let ExprKind::Path(qpath) = &fn_expr.kind;
if let QPath::Resolved(_, path) = qpath;
if let Res::Def(_, did) = path.res;
if blacklist_ids.contains(&did);
then {
span_lint_and_help(
cx,
MEM_UNSAFE_FUNCTIONS,
fn_expr.span,
"use of potentially dangerous memory manipulation function",
None,
"consider using its safe version",
);
}
}
}
159 changes: 159 additions & 0 deletions clippy_lints/src/guidelines/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
mod falliable_memory_allocation;
mod mem_unsafe_functions;
mod passing_string_to_c_functions;
mod untrusted_lib_loading;

use clippy_utils::def_path_def_ids;
use rustc_hir as hir;
use rustc_hir::def_id::{DefId, DefIdSet};
use rustc_hir::intravisit;
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::def_id::LocalDefId;
use rustc_span::Span;

declare_clippy_lint! {
/// ### What it does
/// Checks for direct usage of external functions that modify memory
/// without concerning about memory safety, such as `memcpy`, `strcpy`, `strcat` etc.
///
/// ### Why is this bad?
/// These function can be dangerous when used incorrectly,
/// which could potentially introduce vulnerablities such as buffer overflow to the software.
///
/// ### Example
/// ```rust
/// 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,
"use of potentially dangerous external functions"
}

declare_clippy_lint! {
/// ### What it does
///
/// ### Why is this bad?
///
/// ### Example
/// ```rust
/// // example code where clippy issues a warning
/// ```
/// Use instead:
/// ```rust
/// // example code which does not raise clippy warning
/// ```
#[clippy::version = "1.70.0"]
pub UNTRUSTED_LIB_LOADING,
nursery,
"attempt to load dynamic library from untrusted source"
}

declare_clippy_lint! {
/// ### What it does
///
/// ### Why is this bad?
///
/// ### Example
/// ```rust
/// // example code where clippy issues a warning
/// ```
/// Use instead:
/// ```rust
/// // example code which does not raise clippy warning
/// ```
#[clippy::version = "1.70.0"]
pub PASSING_STRING_TO_C_FUNCTIONS,
nursery,
"passing string or str to extern C function"
}

declare_clippy_lint! {
/// ### What it does
///
/// ### Why is this bad?
///
/// ### Example
/// ```rust
/// // example code where clippy issues a warning
/// ```
/// Use instead:
/// ```rust
/// // example code which does not raise clippy warning
/// ```
#[clippy::version = "1.70.0"]
pub FALLIABLE_MEMORY_ALLOCATION,
nursery,
"memory allocation without checking arguments and result"
}

#[derive(Clone, Default)]
pub struct GuidelineLints {
mem_uns_fns: Vec<String>,
mem_uns_fns_ty_ids: DefIdSet,
}

impl GuidelineLints {
pub fn new(mem_uns_fns: Vec<String>) -> Self {
Self {
mem_uns_fns,
mem_uns_fns_ty_ids: DefIdSet::new(),
}
}
}

impl_lint_pass!(GuidelineLints => [
MEM_UNSAFE_FUNCTIONS,
UNTRUSTED_LIB_LOADING,
PASSING_STRING_TO_C_FUNCTIONS,
FALLIABLE_MEMORY_ALLOCATION,
]);

impl<'tcx> LateLintPass<'tcx> for GuidelineLints {
fn check_fn(
&mut self,
_cx: &LateContext<'tcx>,
_kind: intravisit::FnKind<'tcx>,
_decl: &'tcx hir::FnDecl<'_>,
_body: &'tcx hir::Body<'_>,
_span: Span,
_def_id: LocalDefId,
) {
}

fn check_crate(&mut self, cx: &LateContext<'tcx>) {
// Resolve function names to def_ids from configuration
for uns_fns in &self.mem_uns_fns {
// Path like function names such as `libc::foo` or `aa::bb::cc::bar`,
// this only works with dependencies.
if uns_fns.contains("::") {
let path: Vec<&str> = uns_fns.split("::").collect();
for did in def_path_def_ids(cx, path.as_slice()) {
self.mem_uns_fns_ty_ids.insert(did);
}
}
// Plain function names, then we should take its libc variant into account
else if let Some(did) = libc_fn_def_id(cx, uns_fns) {
self.mem_uns_fns_ty_ids.insert(did);
}
}
}

fn check_item(&mut self, _cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>) {
mem_unsafe_functions::check_foreign_item(item, &self.mem_uns_fns, &mut self.mem_uns_fns_ty_ids);
}

fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
mem_unsafe_functions::check(cx, expr, &self.mem_uns_fns_ty_ids);
}
}

fn libc_fn_def_id(cx: &LateContext<'_>, fn_name: &str) -> Option<DefId> {
let path = &["libc", fn_name];
def_path_def_ids(cx, path).next()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions clippy_lints/src/guidelines/untrusted_lib_loading.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

51 changes: 51 additions & 0 deletions clippy_lints/src/implicit_abi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use rustc_ast::ast::{Item, ItemKind};
use rustc_errors::Applicability;
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
use rustc_session::{declare_lint_pass, declare_tool_lint};

declare_clippy_lint! {
/// ### What it does
/// Checks the external block without explicitly lable its ABI.
///
/// ### Why is this bad?
/// Implicit ABI has negative impact on code readability.
///
/// ### Example
/// ```rust
/// extern {
/// fn c_function();
/// }
/// ```
/// Use instead:
/// ```rust
/// extern "C" {
/// fn c_function();
/// }
/// ```
#[clippy::version = "1.70.0"]
pub IMPLICIT_ABI,
restriction,
"external block with implicit ABI"
}

declare_lint_pass!(ImplicitAbi => [IMPLICIT_ABI]);

impl EarlyLintPass for ImplicitAbi {
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
if let ItemKind::ForeignMod(fm) = &item.kind {
if fm.abi.is_none() {
let extern_span = cx.sess().source_map().span_until_whitespace(item.span);
span_lint_and_sugg(
cx,
IMPLICIT_ABI,
extern_span,
"missing ABI label on extern block",
"explicitly states ABI instead",
"extern \"C\"".to_string(),
Applicability::MachineApplicable,
);
}
}
}
}
Loading

0 comments on commit 4f6422c

Please sign in to comment.