2021-03-27 13:41:55 +00:00
|
|
|
mod must_use;
|
2021-03-27 11:43:59 +00:00
|
|
|
mod not_unsafe_ptr_arg_deref;
|
2021-03-27 13:48:25 +00:00
|
|
|
mod result_unit_err;
|
2021-03-24 14:12:08 +00:00
|
|
|
mod too_many_arguments;
|
2021-03-24 14:20:31 +00:00
|
|
|
mod too_many_lines;
|
2021-03-24 14:12:08 +00:00
|
|
|
|
2020-01-06 16:39:50 +00:00
|
|
|
use rustc_hir as hir;
|
2020-01-09 07:13:22 +00:00
|
|
|
use rustc_hir::intravisit;
|
2021-03-27 13:48:25 +00:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2020-01-11 11:37:08 +00:00
|
|
|
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
2021-03-27 13:48:25 +00:00
|
|
|
use rustc_span::Span;
|
2016-03-08 23:48:10 +00:00
|
|
|
|
2018-03-28 13:24:26 +00:00
|
|
|
declare_clippy_lint! {
|
2019-03-05 16:50:33 +00:00
|
|
|
/// **What it does:** Checks for functions with too many parameters.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** Functions with lots of parameters are considered bad
|
|
|
|
/// style and reduce readability (“what does the 5th parameter mean?”). Consider
|
|
|
|
/// grouping some parameters into a new type.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
2019-08-02 06:13:54 +00:00
|
|
|
/// # struct Color;
|
2019-03-05 16:50:33 +00:00
|
|
|
/// fn foo(x: u32, y: u32, name: &str, c: Color, w: f32, h: f32, a: f32, b: f32) {
|
2019-08-02 06:13:54 +00:00
|
|
|
/// // ..
|
2019-03-05 16:50:33 +00:00
|
|
|
/// }
|
|
|
|
/// ```
|
2016-03-08 23:48:10 +00:00
|
|
|
pub TOO_MANY_ARGUMENTS,
|
2018-03-29 11:41:53 +00:00
|
|
|
complexity,
|
2016-03-08 23:48:10 +00:00
|
|
|
"functions with too many arguments"
|
|
|
|
}
|
|
|
|
|
2019-01-13 15:19:02 +00:00
|
|
|
declare_clippy_lint! {
|
2019-03-05 16:50:33 +00:00
|
|
|
/// **What it does:** Checks for functions with a large amount of lines.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** Functions with a lot of lines are harder to understand
|
|
|
|
/// due to having to look at a larger amount of code to understand what the
|
|
|
|
/// function is doing. Consider splitting the body of the function into
|
|
|
|
/// multiple functions.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
2020-06-09 14:36:01 +00:00
|
|
|
/// ```rust
|
2019-03-05 16:50:33 +00:00
|
|
|
/// fn im_too_long() {
|
2020-06-09 14:36:01 +00:00
|
|
|
/// println!("");
|
|
|
|
/// // ... 100 more LoC
|
|
|
|
/// println!("");
|
2019-03-05 16:50:33 +00:00
|
|
|
/// }
|
|
|
|
/// ```
|
2019-01-13 15:19:02 +00:00
|
|
|
pub TOO_MANY_LINES,
|
|
|
|
pedantic,
|
|
|
|
"functions with too many lines"
|
|
|
|
}
|
|
|
|
|
2018-03-28 13:24:26 +00:00
|
|
|
declare_clippy_lint! {
|
2019-06-12 18:07:10 +00:00
|
|
|
/// **What it does:** Checks for public functions that dereference raw pointer
|
2021-03-30 02:45:54 +00:00
|
|
|
/// arguments but are not marked `unsafe`.
|
2019-03-05 16:50:33 +00:00
|
|
|
///
|
|
|
|
/// **Why is this bad?** The function should probably be marked `unsafe`, since
|
|
|
|
/// for an arbitrary raw pointer, there is no way of telling for sure if it is
|
|
|
|
/// valid.
|
|
|
|
///
|
|
|
|
/// **Known problems:**
|
|
|
|
///
|
|
|
|
/// * It does not check functions recursively so if the pointer is passed to a
|
|
|
|
/// private non-`unsafe` function which does the dereferencing, the lint won't
|
|
|
|
/// trigger.
|
|
|
|
/// * It only checks for arguments whose type are raw pointers, not raw pointers
|
|
|
|
/// got from an argument in some other way (`fn foo(bar: &[*const u8])` or
|
|
|
|
/// `some_argument.get_raw_ptr()`).
|
|
|
|
///
|
|
|
|
/// **Example:**
|
2020-06-09 14:36:01 +00:00
|
|
|
/// ```rust,ignore
|
|
|
|
/// // Bad
|
2019-03-05 16:50:33 +00:00
|
|
|
/// pub fn foo(x: *const u8) {
|
|
|
|
/// println!("{}", unsafe { *x });
|
|
|
|
/// }
|
2020-06-09 14:36:01 +00:00
|
|
|
///
|
|
|
|
/// // Good
|
|
|
|
/// pub unsafe fn foo(x: *const u8) {
|
|
|
|
/// println!("{}", unsafe { *x });
|
|
|
|
/// }
|
2019-03-05 16:50:33 +00:00
|
|
|
/// ```
|
2016-06-07 14:55:55 +00:00
|
|
|
pub NOT_UNSAFE_PTR_ARG_DEREF,
|
2018-03-28 13:24:26 +00:00
|
|
|
correctness,
|
2016-06-07 14:55:55 +00:00
|
|
|
"public functions dereferencing raw pointer arguments but not marked `unsafe`"
|
|
|
|
}
|
|
|
|
|
2019-09-18 06:37:41 +00:00
|
|
|
declare_clippy_lint! {
|
|
|
|
/// **What it does:** Checks for a [`#[must_use]`] attribute on
|
|
|
|
/// unit-returning functions and methods.
|
|
|
|
///
|
|
|
|
/// [`#[must_use]`]: https://doc.rust-lang.org/reference/attributes/diagnostics.html#the-must_use-attribute
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** Unit values are useless. The attribute is likely
|
|
|
|
/// a remnant of a refactoring that removed the return type.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Examples:**
|
|
|
|
/// ```rust
|
|
|
|
/// #[must_use]
|
|
|
|
/// fn useless() { }
|
|
|
|
/// ```
|
|
|
|
pub MUST_USE_UNIT,
|
|
|
|
style,
|
|
|
|
"`#[must_use]` attribute on a unit-returning function / method"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
|
|
|
/// **What it does:** Checks for a [`#[must_use]`] attribute without
|
|
|
|
/// further information on functions and methods that return a type already
|
|
|
|
/// marked as `#[must_use]`.
|
|
|
|
///
|
|
|
|
/// [`#[must_use]`]: https://doc.rust-lang.org/reference/attributes/diagnostics.html#the-must_use-attribute
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** The attribute isn't needed. Not using the result
|
|
|
|
/// will already be reported. Alternatively, one can add some text to the
|
|
|
|
/// attribute to improve the lint message.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Examples:**
|
|
|
|
/// ```rust
|
|
|
|
/// #[must_use]
|
|
|
|
/// fn double_must_use() -> Result<(), ()> {
|
|
|
|
/// unimplemented!();
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub DOUBLE_MUST_USE,
|
|
|
|
style,
|
|
|
|
"`#[must_use]` attribute on a `#[must_use]`-returning function / method"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
|
|
|
/// **What it does:** Checks for public functions that have no
|
|
|
|
/// [`#[must_use]`] attribute, but return something not already marked
|
|
|
|
/// must-use, have no mutable arg and mutate no statics.
|
|
|
|
///
|
|
|
|
/// [`#[must_use]`]: https://doc.rust-lang.org/reference/attributes/diagnostics.html#the-must_use-attribute
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** Not bad at all, this lint just shows places where
|
|
|
|
/// you could add the attribute.
|
|
|
|
///
|
|
|
|
/// **Known problems:** The lint only checks the arguments for mutable
|
|
|
|
/// types without looking if they are actually changed. On the other hand,
|
|
|
|
/// it also ignores a broad range of potentially interesting side effects,
|
|
|
|
/// because we cannot decide whether the programmer intends the function to
|
|
|
|
/// be called for the side effect or the result. Expect many false
|
|
|
|
/// positives. At least we don't lint if the result type is unit or already
|
|
|
|
/// `#[must_use]`.
|
|
|
|
///
|
|
|
|
/// **Examples:**
|
|
|
|
/// ```rust
|
|
|
|
/// // this could be annotated with `#[must_use]`.
|
|
|
|
/// fn id<T>(t: T) -> T { t }
|
|
|
|
/// ```
|
|
|
|
pub MUST_USE_CANDIDATE,
|
|
|
|
pedantic,
|
|
|
|
"function or method that could take a `#[must_use]` attribute"
|
|
|
|
}
|
|
|
|
|
2020-10-23 20:16:59 +00:00
|
|
|
declare_clippy_lint! {
|
|
|
|
/// **What it does:** Checks for public functions that return a `Result`
|
|
|
|
/// with an `Err` type of `()`. It suggests using a custom type that
|
2021-03-30 02:45:54 +00:00
|
|
|
/// implements `std::error::Error`.
|
2020-10-23 20:16:59 +00:00
|
|
|
///
|
|
|
|
/// **Why is this bad?** Unit does not implement `Error` and carries no
|
|
|
|
/// further information about what went wrong.
|
|
|
|
///
|
|
|
|
/// **Known problems:** Of course, this lint assumes that `Result` is used
|
|
|
|
/// for a fallible operation (which is after all the intended use). However
|
|
|
|
/// code may opt to (mis)use it as a basic two-variant-enum. In that case,
|
|
|
|
/// the suggestion is misguided, and the code should use a custom enum
|
|
|
|
/// instead.
|
|
|
|
///
|
|
|
|
/// **Examples:**
|
|
|
|
/// ```rust
|
|
|
|
/// pub fn read_u8() -> Result<u8, ()> { Err(()) }
|
|
|
|
/// ```
|
|
|
|
/// should become
|
|
|
|
/// ```rust,should_panic
|
|
|
|
/// use std::fmt;
|
|
|
|
///
|
|
|
|
/// #[derive(Debug)]
|
|
|
|
/// pub struct EndOfStream;
|
|
|
|
///
|
|
|
|
/// impl fmt::Display for EndOfStream {
|
|
|
|
/// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
/// write!(f, "End of Stream")
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// impl std::error::Error for EndOfStream { }
|
|
|
|
///
|
|
|
|
/// pub fn read_u8() -> Result<u8, EndOfStream> { Err(EndOfStream) }
|
|
|
|
///# fn main() {
|
|
|
|
///# read_u8().unwrap();
|
|
|
|
///# }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Note that there are crates that simplify creating the error type, e.g.
|
|
|
|
/// [`thiserror`](https://docs.rs/thiserror).
|
|
|
|
pub RESULT_UNIT_ERR,
|
|
|
|
style,
|
|
|
|
"public function returning `Result` with an `Err` type of `()`"
|
|
|
|
}
|
|
|
|
|
2017-08-09 07:30:56 +00:00
|
|
|
#[derive(Copy, Clone)]
|
2016-03-08 23:48:10 +00:00
|
|
|
pub struct Functions {
|
2021-03-24 14:12:08 +00:00
|
|
|
too_many_arguments_threshold: u64,
|
|
|
|
too_many_lines_threshold: u64,
|
2016-03-08 23:48:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Functions {
|
2021-03-24 14:12:08 +00:00
|
|
|
pub fn new(too_many_arguments_threshold: u64, too_many_lines_threshold: u64) -> Self {
|
|
|
|
Self {
|
|
|
|
too_many_arguments_threshold,
|
|
|
|
too_many_lines_threshold,
|
|
|
|
}
|
2016-03-08 23:48:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-18 06:37:41 +00:00
|
|
|
impl_lint_pass!(Functions => [
|
|
|
|
TOO_MANY_ARGUMENTS,
|
|
|
|
TOO_MANY_LINES,
|
|
|
|
NOT_UNSAFE_PTR_ARG_DEREF,
|
|
|
|
MUST_USE_UNIT,
|
|
|
|
DOUBLE_MUST_USE,
|
|
|
|
MUST_USE_CANDIDATE,
|
2020-10-23 20:16:59 +00:00
|
|
|
RESULT_UNIT_ERR,
|
2019-09-18 06:37:41 +00:00
|
|
|
]);
|
2016-03-08 23:48:10 +00:00
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for Functions {
|
2016-12-21 09:25:14 +00:00
|
|
|
fn check_fn(
|
2016-12-21 11:14:54 +00:00
|
|
|
&mut self,
|
2020-06-25 20:41:36 +00:00
|
|
|
cx: &LateContext<'tcx>,
|
2016-12-21 11:14:54 +00:00
|
|
|
kind: intravisit::FnKind<'tcx>,
|
2019-12-30 04:02:10 +00:00
|
|
|
decl: &'tcx hir::FnDecl<'_>,
|
2019-12-22 14:42:41 +00:00
|
|
|
body: &'tcx hir::Body<'_>,
|
2016-12-21 11:14:54 +00:00
|
|
|
span: Span,
|
2019-02-20 10:11:11 +00:00
|
|
|
hir_id: hir::HirId,
|
2016-12-21 09:25:14 +00:00
|
|
|
) {
|
2021-03-24 14:12:08 +00:00
|
|
|
too_many_arguments::check_fn(cx, kind, decl, span, hir_id, self.too_many_arguments_threshold);
|
2021-03-27 13:48:25 +00:00
|
|
|
too_many_lines::check_fn(cx, span, body, self.too_many_lines_threshold);
|
2021-03-27 11:43:59 +00:00
|
|
|
not_unsafe_ptr_arg_deref::check_fn(cx, kind, decl, body, hir_id);
|
2016-03-08 23:48:10 +00:00
|
|
|
}
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>) {
|
2021-03-27 13:41:55 +00:00
|
|
|
must_use::check_item(cx, item);
|
2021-03-27 13:48:25 +00:00
|
|
|
result_unit_err::check_item(cx, item);
|
2019-09-18 06:37:41 +00:00
|
|
|
}
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::ImplItem<'_>) {
|
2021-03-27 13:41:55 +00:00
|
|
|
must_use::check_impl_item(cx, item);
|
2021-03-27 13:48:25 +00:00
|
|
|
result_unit_err::check_impl_item(cx, item);
|
2019-09-18 06:37:41 +00:00
|
|
|
}
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
fn check_trait_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::TraitItem<'_>) {
|
2021-03-24 14:12:08 +00:00
|
|
|
too_many_arguments::check_trait_item(cx, item, self.too_many_arguments_threshold);
|
2021-03-27 11:43:59 +00:00
|
|
|
not_unsafe_ptr_arg_deref::check_trait_item(cx, item);
|
2021-03-27 13:41:55 +00:00
|
|
|
must_use::check_trait_item(cx, item);
|
2021-03-27 13:48:25 +00:00
|
|
|
result_unit_err::check_trait_item(cx, item);
|
2020-10-23 20:16:59 +00:00
|
|
|
}
|
|
|
|
}
|