2019-05-01 19:32:54 +00:00
|
|
|
use std::convert::TryFrom;
|
|
|
|
|
|
|
|
use crate::utils::{iter_input_pats, snippet, snippet_opt, span_lint, type_is_unsafe_function};
|
2018-11-27 20:14:15 +00:00
|
|
|
use matches::matches;
|
2018-12-29 15:04:45 +00:00
|
|
|
use rustc::hir;
|
2019-05-04 00:03:12 +00:00
|
|
|
use rustc::hir::def::Res;
|
2018-12-29 15:04:45 +00:00
|
|
|
use rustc::hir::intravisit;
|
2019-01-19 22:35:32 +00:00
|
|
|
use rustc::lint::{in_external_macro, LateContext, LateLintPass, LintArray, LintContext, LintPass};
|
2018-12-29 15:04:45 +00:00
|
|
|
use rustc::ty;
|
2019-04-08 20:43:55 +00:00
|
|
|
use rustc::{declare_tool_lint, impl_lint_pass};
|
2018-12-29 15:04:45 +00:00
|
|
|
use rustc_data_structures::fx::FxHashSet;
|
|
|
|
use rustc_target::spec::abi::Abi;
|
2019-05-01 19:32:54 +00:00
|
|
|
use syntax::source_map::{BytePos, 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
|
|
|
|
/// fn foo(x: u32, y: u32, name: &str, c: Color, w: f32, h: f32, a: f32, b: f32) {
|
|
|
|
/// ..
|
|
|
|
/// }
|
|
|
|
/// ```
|
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:**
|
|
|
|
/// ``` rust
|
|
|
|
/// fn im_too_long() {
|
|
|
|
/// println!("");
|
|
|
|
/// // ... 100 more LoC
|
|
|
|
/// println!("");
|
|
|
|
/// }
|
|
|
|
/// ```
|
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-03-05 16:50:33 +00:00
|
|
|
/// **What it does:** Checks for public functions that dereferences raw pointer
|
|
|
|
/// arguments but are not marked unsafe.
|
|
|
|
///
|
|
|
|
/// **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:**
|
|
|
|
/// ```rust
|
|
|
|
/// pub fn foo(x: *const u8) {
|
|
|
|
/// println!("{}", unsafe { *x });
|
|
|
|
/// }
|
|
|
|
/// ```
|
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`"
|
|
|
|
}
|
|
|
|
|
2017-08-09 07:30:56 +00:00
|
|
|
#[derive(Copy, Clone)]
|
2016-03-08 23:48:10 +00:00
|
|
|
pub struct Functions {
|
|
|
|
threshold: u64,
|
2019-01-13 21:53:26 +00:00
|
|
|
max_lines: u64,
|
2016-03-08 23:48:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Functions {
|
2019-01-13 15:19:02 +00:00
|
|
|
pub fn new(threshold: u64, max_lines: u64) -> Self {
|
2019-01-13 21:53:26 +00:00
|
|
|
Self { threshold, max_lines }
|
2016-03-08 23:48:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-08 20:43:55 +00:00
|
|
|
impl_lint_pass!(Functions => [TOO_MANY_ARGUMENTS, TOO_MANY_LINES, NOT_UNSAFE_PTR_ARG_DEREF]);
|
2016-03-08 23:48:10 +00:00
|
|
|
|
2016-12-07 12:13:40 +00:00
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Functions {
|
2016-12-21 09:25:14 +00:00
|
|
|
fn check_fn(
|
2016-12-21 11:14:54 +00:00
|
|
|
&mut self,
|
|
|
|
cx: &LateContext<'a, 'tcx>,
|
|
|
|
kind: intravisit::FnKind<'tcx>,
|
|
|
|
decl: &'tcx hir::FnDecl,
|
2017-01-04 04:40:42 +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
|
|
|
) {
|
2019-02-24 14:16:16 +00:00
|
|
|
let is_impl = if let Some(hir::Node::Item(item)) = cx
|
|
|
|
.tcx
|
|
|
|
.hir()
|
|
|
|
.find_by_hir_id(cx.tcx.hir().get_parent_node_by_hir_id(hir_id))
|
2019-02-20 10:11:11 +00:00
|
|
|
{
|
2018-07-16 13:07:39 +00:00
|
|
|
matches!(item.node, hir::ItemKind::Impl(_, _, _, _, Some(_), _, _))
|
2016-06-07 14:55:55 +00:00
|
|
|
} else {
|
|
|
|
false
|
|
|
|
};
|
|
|
|
|
|
|
|
let unsafety = match kind {
|
2018-06-24 13:32:40 +00:00
|
|
|
hir::intravisit::FnKind::ItemFn(_, _, hir::FnHeader { unsafety, .. }, _, _) => unsafety,
|
|
|
|
hir::intravisit::FnKind::Method(_, sig, _, _) => sig.header.unsafety,
|
2016-06-07 14:55:55 +00:00
|
|
|
hir::intravisit::FnKind::Closure(_) => return,
|
|
|
|
};
|
|
|
|
|
|
|
|
// don't warn for implementations, it's not their fault
|
|
|
|
if !is_impl {
|
2016-08-08 15:21:47 +00:00
|
|
|
// don't lint extern functions decls, it's not their fault either
|
|
|
|
match kind {
|
2018-11-27 20:14:15 +00:00
|
|
|
hir::intravisit::FnKind::Method(
|
|
|
|
_,
|
|
|
|
&hir::MethodSig {
|
|
|
|
header: hir::FnHeader { abi: Abi::Rust, .. },
|
|
|
|
..
|
|
|
|
},
|
|
|
|
_,
|
|
|
|
_,
|
|
|
|
)
|
|
|
|
| hir::intravisit::FnKind::ItemFn(_, _, hir::FnHeader { abi: Abi::Rust, .. }, _, _) => {
|
|
|
|
self.check_arg_number(cx, decl, span)
|
|
|
|
},
|
2016-08-08 15:21:47 +00:00
|
|
|
_ => {},
|
|
|
|
}
|
2016-03-08 23:48:10 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 09:39:33 +00:00
|
|
|
self.check_raw_ptr(cx, unsafety, decl, body, hir_id);
|
2019-02-23 17:29:30 +00:00
|
|
|
self.check_line_number(cx, span, body);
|
2016-03-08 23:48:10 +00:00
|
|
|
}
|
|
|
|
|
2016-12-07 12:13:40 +00:00
|
|
|
fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx hir::TraitItem) {
|
2017-01-04 22:14:22 +00:00
|
|
|
if let hir::TraitItemKind::Method(ref sig, ref eid) = item.node {
|
2016-08-08 15:21:47 +00:00
|
|
|
// don't lint extern functions decls, it's not their fault
|
2018-06-24 13:32:40 +00:00
|
|
|
if sig.header.abi == Abi::Rust {
|
2016-08-08 15:21:47 +00:00
|
|
|
self.check_arg_number(cx, &sig.decl, item.span);
|
|
|
|
}
|
2016-06-07 14:55:55 +00:00
|
|
|
|
2017-01-04 22:14:22 +00:00
|
|
|
if let hir::TraitMethod::Provided(eid) = *eid {
|
2018-12-08 00:56:03 +00:00
|
|
|
let body = cx.tcx.hir().body(eid);
|
2019-02-27 09:39:33 +00:00
|
|
|
self.check_raw_ptr(cx, sig.header.unsafety, &sig.decl, body, item.hir_id);
|
2016-06-07 14:55:55 +00:00
|
|
|
}
|
2016-03-08 23:48:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-07 12:13:40 +00:00
|
|
|
impl<'a, 'tcx> Functions {
|
2018-07-23 11:01:12 +00:00
|
|
|
fn check_arg_number(self, cx: &LateContext<'_, '_>, decl: &hir::FnDecl, span: Span) {
|
2019-05-01 19:32:54 +00:00
|
|
|
// Remove the function body from the span. We can't use `SourceMap::def_span` because the
|
|
|
|
// argument list might span multiple lines.
|
|
|
|
let span = if let Some(snippet) = snippet_opt(cx, span) {
|
|
|
|
let snippet = snippet.split('{').nth(0).unwrap_or("").trim_end();
|
|
|
|
if snippet.is_empty() {
|
|
|
|
span
|
|
|
|
} else {
|
|
|
|
span.with_hi(BytePos(span.lo().0 + u32::try_from(snippet.len()).unwrap()))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
span
|
|
|
|
};
|
|
|
|
|
2016-03-08 23:48:10 +00:00
|
|
|
let args = decl.inputs.len() as u64;
|
|
|
|
if args > self.threshold {
|
2017-08-09 07:30:56 +00:00
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
TOO_MANY_ARGUMENTS,
|
|
|
|
span,
|
|
|
|
&format!("this function has too many arguments ({}/{})", args, self.threshold),
|
|
|
|
);
|
2016-03-08 23:48:10 +00:00
|
|
|
}
|
|
|
|
}
|
2016-06-07 14:55:55 +00:00
|
|
|
|
2019-02-23 17:29:30 +00:00
|
|
|
fn check_line_number(self, cx: &LateContext<'_, '_>, span: Span, body: &'tcx hir::Body) {
|
2019-01-19 22:35:32 +00:00
|
|
|
if in_external_macro(cx.sess(), span) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-23 17:29:30 +00:00
|
|
|
let code_snippet = snippet(cx, body.value.span, "..");
|
2019-01-13 21:26:03 +00:00
|
|
|
let mut line_count: u64 = 0;
|
2019-01-13 15:19:02 +00:00
|
|
|
let mut in_comment = false;
|
2019-01-13 21:26:03 +00:00
|
|
|
let mut code_in_line;
|
|
|
|
|
|
|
|
// Skip the surrounding function decl.
|
2019-02-01 04:50:55 +00:00
|
|
|
let start_brace_idx = match code_snippet.find('{') {
|
2019-01-13 21:26:03 +00:00
|
|
|
Some(i) => i + 1,
|
2019-01-13 21:53:26 +00:00
|
|
|
None => 0,
|
2019-01-13 21:26:03 +00:00
|
|
|
};
|
2019-02-01 04:50:55 +00:00
|
|
|
let end_brace_idx = match code_snippet.find('}') {
|
2019-01-13 21:26:03 +00:00
|
|
|
Some(i) => i,
|
2019-01-13 21:53:26 +00:00
|
|
|
None => code_snippet.len(),
|
2019-01-13 21:26:03 +00:00
|
|
|
};
|
|
|
|
let function_lines = code_snippet[start_brace_idx..end_brace_idx].lines();
|
|
|
|
|
|
|
|
for mut line in function_lines {
|
|
|
|
code_in_line = false;
|
|
|
|
loop {
|
|
|
|
line = line.trim_start();
|
2019-01-13 21:53:26 +00:00
|
|
|
if line.is_empty() {
|
|
|
|
break;
|
|
|
|
}
|
2019-01-13 21:26:03 +00:00
|
|
|
if in_comment {
|
2019-01-13 15:19:02 +00:00
|
|
|
match line.find("*/") {
|
|
|
|
Some(i) => {
|
2019-01-13 21:26:03 +00:00
|
|
|
line = &line[i + 2..];
|
|
|
|
in_comment = false;
|
|
|
|
continue;
|
2019-01-13 15:19:02 +00:00
|
|
|
},
|
2019-01-13 21:53:26 +00:00
|
|
|
None => break,
|
2019-01-13 15:19:02 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-01-13 21:26:03 +00:00
|
|
|
let multi_idx = match line.find("/*") {
|
|
|
|
Some(i) => i,
|
2019-01-13 21:53:26 +00:00
|
|
|
None => line.len(),
|
2019-01-13 21:26:03 +00:00
|
|
|
};
|
|
|
|
let single_idx = match line.find("//") {
|
|
|
|
Some(i) => i,
|
2019-01-13 21:53:26 +00:00
|
|
|
None => line.len(),
|
2019-01-13 21:26:03 +00:00
|
|
|
};
|
|
|
|
code_in_line |= multi_idx > 0 && single_idx > 0;
|
|
|
|
// Implies multi_idx is below line.len()
|
|
|
|
if multi_idx < single_idx {
|
|
|
|
line = &line[multi_idx + 2..];
|
|
|
|
in_comment = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
2019-01-13 15:19:02 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-13 21:53:26 +00:00
|
|
|
if code_in_line {
|
|
|
|
line_count += 1;
|
|
|
|
}
|
2019-01-13 15:19:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if line_count > self.max_lines {
|
2019-01-13 21:53:26 +00:00
|
|
|
span_lint(cx, TOO_MANY_LINES, span, "This function has a large number of lines.")
|
2019-01-13 15:19:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-21 09:25:14 +00:00
|
|
|
fn check_raw_ptr(
|
2018-05-31 18:15:48 +00:00
|
|
|
self,
|
2016-12-21 11:14:54 +00:00
|
|
|
cx: &LateContext<'a, 'tcx>,
|
|
|
|
unsafety: hir::Unsafety,
|
|
|
|
decl: &'tcx hir::FnDecl,
|
2017-01-04 21:46:41 +00:00
|
|
|
body: &'tcx hir::Body,
|
2019-02-27 09:39:33 +00:00
|
|
|
hir_id: hir::HirId,
|
2016-12-21 09:25:14 +00:00
|
|
|
) {
|
2017-01-04 21:46:41 +00:00
|
|
|
let expr = &body.value;
|
2019-03-23 22:37:38 +00:00
|
|
|
if unsafety == hir::Unsafety::Normal && cx.access_levels.is_exported(hir_id) {
|
2017-01-04 23:53:16 +00:00
|
|
|
let raw_ptrs = iter_input_pats(decl, body)
|
|
|
|
.zip(decl.inputs.iter())
|
|
|
|
.filter_map(|(arg, ty)| raw_ptr_arg(arg, ty))
|
2018-09-11 23:34:52 +00:00
|
|
|
.collect::<FxHashSet<_>>();
|
2016-06-07 14:55:55 +00:00
|
|
|
|
|
|
|
if !raw_ptrs.is_empty() {
|
2017-08-21 10:57:33 +00:00
|
|
|
let tables = cx.tcx.body_tables(body.id());
|
2016-06-07 14:55:55 +00:00
|
|
|
let mut v = DerefVisitor {
|
2018-03-15 15:07:15 +00:00
|
|
|
cx,
|
2016-06-07 14:55:55 +00:00
|
|
|
ptrs: raw_ptrs,
|
2017-08-21 10:57:33 +00:00
|
|
|
tables,
|
2016-06-07 14:55:55 +00:00
|
|
|
};
|
|
|
|
|
2016-11-16 20:57:56 +00:00
|
|
|
hir::intravisit::walk_expr(&mut v, expr);
|
2016-06-07 14:55:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-07 20:51:05 +00:00
|
|
|
fn raw_ptr_arg(arg: &hir::Arg, ty: &hir::Ty) -> Option<hir::HirId> {
|
|
|
|
if let (&hir::PatKind::Binding(_, id, _, _), &hir::TyKind::Ptr(_)) = (&arg.pat.node, &ty.node) {
|
2017-09-12 12:26:40 +00:00
|
|
|
Some(id)
|
2016-06-07 14:55:55 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct DerefVisitor<'a, 'tcx: 'a> {
|
|
|
|
cx: &'a LateContext<'a, 'tcx>,
|
2019-03-07 20:51:05 +00:00
|
|
|
ptrs: FxHashSet<hir::HirId>,
|
2017-08-21 10:57:33 +00:00
|
|
|
tables: &'a ty::TypeckTables<'tcx>,
|
2016-06-07 14:55:55 +00:00
|
|
|
}
|
|
|
|
|
2016-12-06 10:32:21 +00:00
|
|
|
impl<'a, 'tcx> hir::intravisit::Visitor<'tcx> for DerefVisitor<'a, 'tcx> {
|
|
|
|
fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
|
2016-06-07 15:29:22 +00:00
|
|
|
match expr.node {
|
2018-07-12 07:30:57 +00:00
|
|
|
hir::ExprKind::Call(ref f, ref args) => {
|
2017-08-21 10:57:33 +00:00
|
|
|
let ty = self.tables.expr_ty(f);
|
2016-06-07 15:29:22 +00:00
|
|
|
|
2017-06-29 13:38:25 +00:00
|
|
|
if type_is_unsafe_function(self.cx, ty) {
|
2016-06-07 15:29:22 +00:00
|
|
|
for arg in args {
|
|
|
|
self.check_arg(arg);
|
|
|
|
}
|
|
|
|
}
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2018-07-12 07:30:57 +00:00
|
|
|
hir::ExprKind::MethodCall(_, _, ref args) => {
|
2019-04-10 14:18:02 +00:00
|
|
|
let def_id = self.tables.type_dependent_def_id(expr.hir_id).unwrap();
|
2017-06-02 04:13:04 +00:00
|
|
|
let base_type = self.cx.tcx.type_of(def_id);
|
2016-06-07 14:55:55 +00:00
|
|
|
|
2017-06-29 13:38:25 +00:00
|
|
|
if type_is_unsafe_function(self.cx, base_type) {
|
2016-06-07 15:29:22 +00:00
|
|
|
for arg in args {
|
|
|
|
self.check_arg(arg);
|
|
|
|
}
|
2016-06-07 14:55:55 +00:00
|
|
|
}
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2018-07-12 07:30:57 +00:00
|
|
|
hir::ExprKind::Unary(hir::UnDeref, ref ptr) => self.check_arg(ptr),
|
2016-06-07 15:29:22 +00:00
|
|
|
_ => (),
|
2016-06-07 14:55:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hir::intravisit::walk_expr(self, expr);
|
|
|
|
}
|
2016-12-06 10:32:21 +00:00
|
|
|
fn nested_visit_map<'this>(&'this mut self) -> intravisit::NestedVisitorMap<'this, 'tcx> {
|
2017-05-12 10:02:42 +00:00
|
|
|
intravisit::NestedVisitorMap::None
|
2016-12-06 10:32:21 +00:00
|
|
|
}
|
2016-03-08 23:48:10 +00:00
|
|
|
}
|
2016-06-07 15:29:22 +00:00
|
|
|
|
|
|
|
impl<'a, 'tcx: 'a> DerefVisitor<'a, 'tcx> {
|
|
|
|
fn check_arg(&self, ptr: &hir::Expr) {
|
2018-07-12 07:30:57 +00:00
|
|
|
if let hir::ExprKind::Path(ref qpath) = ptr.node {
|
2019-05-04 00:03:12 +00:00
|
|
|
if let Res::Local(id) = self.cx.tables.qpath_res(qpath, ptr.hir_id) {
|
2019-04-14 20:09:17 +00:00
|
|
|
if self.ptrs.contains(&id) {
|
2017-09-12 12:26:40 +00:00
|
|
|
span_lint(
|
|
|
|
self.cx,
|
|
|
|
NOT_UNSAFE_PTR_ARG_DEREF,
|
|
|
|
ptr.span,
|
|
|
|
"this public function dereferences a raw pointer but is not marked `unsafe`",
|
|
|
|
);
|
|
|
|
}
|
2016-06-07 15:29:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|