Auto merge of #8727 - Serial-ATA:lint-large-includes, r=xFrednet

Add `large_include_file` lint

changelog: Add [`large_include_file`] lint
closes #7005
This commit is contained in:
bors 2022-04-25 17:29:27 +00:00
commit 760f293d79
11 changed files with 136 additions and 1 deletions

View file

@ -0,0 +1,86 @@
use clippy_utils::diagnostics::span_lint_and_note;
use clippy_utils::is_lint_allowed;
use clippy_utils::macros::root_macro_call_first_node;
use rustc_ast::LitKind;
use rustc_hir::Expr;
use rustc_hir::ExprKind;
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::sym;
declare_clippy_lint! {
/// ### What it does
/// Checks for the inclusion of large files via `include_bytes!()`
/// and `include_str!()`
///
/// ### Why is this bad?
/// Including large files can increase the size of the binary
///
/// ### Example
/// ```rust,ignore
/// let included_str = include_str!("very_large_file.txt");
/// let included_bytes = include_bytes!("very_large_file.txt);
/// ```
///
/// Instead, you can load the file at runtime:
/// ```rust,ignore
/// use std::fs;
///
/// let string = fs::read_to_string("very_large_file.txt")?;
/// let bytes = fs::read("very_large_file.txt")?;
/// ```
#[clippy::version = "1.62.0"]
pub LARGE_INCLUDE_FILE,
restriction,
"including a large file"
}
pub struct LargeIncludeFile {
max_file_size: u64,
}
impl LargeIncludeFile {
#[must_use]
pub fn new(max_file_size: u64) -> Self {
Self { max_file_size }
}
}
impl_lint_pass!(LargeIncludeFile => [LARGE_INCLUDE_FILE]);
impl LateLintPass<'_> for LargeIncludeFile {
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &'_ Expr<'_>) {
if_chain! {
if let Some(macro_call) = root_macro_call_first_node(cx, expr);
if !is_lint_allowed(cx, LARGE_INCLUDE_FILE, expr.hir_id);
if cx.tcx.is_diagnostic_item(sym::include_bytes_macro, macro_call.def_id)
|| cx.tcx.is_diagnostic_item(sym::include_str_macro, macro_call.def_id);
if let ExprKind::Lit(lit) = &expr.kind;
then {
let len = match &lit.node {
// include_bytes
LitKind::ByteStr(bstr) => bstr.len(),
// include_str
LitKind::Str(sym, _) => sym.as_str().len(),
_ => return,
};
if len as u64 <= self.max_file_size {
return;
}
span_lint_and_note(
cx,
LARGE_INCLUDE_FILE,
expr.span,
"attempted to include a large file",
None,
&format!(
"the configuration allows a maximum size of {} bytes",
self.max_file_size
),
);
}
}
}
}

View file

@ -209,6 +209,7 @@ store.register_lints(&[
iter_not_returning_iterator::ITER_NOT_RETURNING_ITERATOR,
large_const_arrays::LARGE_CONST_ARRAYS,
large_enum_variant::LARGE_ENUM_VARIANT,
large_include_file::LARGE_INCLUDE_FILE,
large_stack_arrays::LARGE_STACK_ARRAYS,
len_zero::COMPARISON_TO_EMPTY,
len_zero::LEN_WITHOUT_IS_EMPTY,

View file

@ -27,6 +27,7 @@ store.register_group(true, "clippy::restriction", Some("clippy_restriction"), ve
LintId::of(indexing_slicing::INDEXING_SLICING),
LintId::of(inherent_impl::MULTIPLE_INHERENT_IMPL),
LintId::of(integer_division::INTEGER_DIVISION),
LintId::of(large_include_file::LARGE_INCLUDE_FILE),
LintId::of(let_underscore::LET_UNDERSCORE_MUST_USE),
LintId::of(literal_representation::DECIMAL_LITERAL_REPRESENTATION),
LintId::of(map_err_ignore::MAP_ERR_IGNORE),

View file

@ -262,6 +262,7 @@ mod items_after_statements;
mod iter_not_returning_iterator;
mod large_const_arrays;
mod large_enum_variant;
mod large_include_file;
mod large_stack_arrays;
mod len_zero;
mod let_if_seq;
@ -884,6 +885,8 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
store.register_early_pass(|| Box::new(pub_use::PubUse));
store.register_late_pass(|| Box::new(format_push_string::FormatPushString));
store.register_late_pass(|| Box::new(bytes_count_to_len::BytesCountToLen));
let max_include_file_size = conf.max_include_file_size;
store.register_late_pass(move || Box::new(large_include_file::LargeIncludeFile::new(max_include_file_size)));
// add lints here, do not remove this comment, it's used in `new_lint`
}

View file

@ -312,6 +312,10 @@ define_Conf! {
(max_suggested_slice_pattern_length: u64 = 3),
/// Lint: AWAIT_HOLDING_INVALID_TYPE
(await_holding_invalid_types: Vec<crate::utils::conf::DisallowedType> = Vec::new()),
/// Lint: LARGE_INCLUDE_FILE.
///
/// The maximum size of a file included via `include_bytes!()` or `include_str!()`, in bytes
(max_include_file_size: u64 = 1_000_000),
}
/// Search for the configuration file.