2024-07-11 23:00:03 -05:00
|
|
|
use clippy_config::Conf;
|
2022-05-05 09:12:52 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_note;
|
|
|
|
use clippy_utils::macros::root_macro_call_first_node;
|
|
|
|
use rustc_ast::LitKind;
|
2023-07-17 03:19:29 -05:00
|
|
|
use rustc_hir::{Expr, ExprKind};
|
2022-05-05 09:12:52 -05:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2023-12-01 11:21:58 -06:00
|
|
|
use rustc_session::impl_lint_pass;
|
2022-05-05 09:12:52 -05:00
|
|
|
use rustc_span::sym;
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
|
|
|
/// ### What it does
|
|
|
|
/// Checks for the inclusion of large files via `include_bytes!()`
|
For restriction lints, replace “Why is this bad?” with “Why restrict this?”
The `restriction` group contains many lints which are not about
necessarily “bad” things, but style choices — perhaps even style choices
which contradict conventional Rust style — or are otherwise very
situational. This results in silly wording like “Why is this bad?
It isn't, but ...”, which I’ve seen confuse a newcomer at least once.
To improve this situation, this commit replaces the “Why is this bad?”
section heading with “Why restrict this?”, for most, but not all,
restriction lints. I left alone the ones whose placement in the
restriction group is more incidental.
In order to make this make sense, I had to remove the “It isn't, but”
texts from the contents of the sections. Sometimes further changes
were needed, or there were obvious fixes to make, and I went ahead
and made those changes without attempting to split them into another
commit, even though many of them are not strictly necessary for the
“Why restrict this?” project.
2024-05-23 00:21:01 -05:00
|
|
|
/// or `include_str!()`.
|
2022-05-05 09:12:52 -05:00
|
|
|
///
|
For restriction lints, replace “Why is this bad?” with “Why restrict this?”
The `restriction` group contains many lints which are not about
necessarily “bad” things, but style choices — perhaps even style choices
which contradict conventional Rust style — or are otherwise very
situational. This results in silly wording like “Why is this bad?
It isn't, but ...”, which I’ve seen confuse a newcomer at least once.
To improve this situation, this commit replaces the “Why is this bad?”
section heading with “Why restrict this?”, for most, but not all,
restriction lints. I left alone the ones whose placement in the
restriction group is more incidental.
In order to make this make sense, I had to remove the “It isn't, but”
texts from the contents of the sections. Sometimes further changes
were needed, or there were obvious fixes to make, and I went ahead
and made those changes without attempting to split them into another
commit, even though many of them are not strictly necessary for the
“Why restrict this?” project.
2024-05-23 00:21:01 -05:00
|
|
|
/// ### Why restrict this?
|
|
|
|
/// Including large files can undesirably increase the size of the binary produced by the compiler.
|
|
|
|
/// This lint may be used to catch mistakes where an unexpectedly large file is included, or
|
|
|
|
/// temporarily to obtain a list of all large files.
|
2022-05-05 09:12:52 -05:00
|
|
|
///
|
|
|
|
/// ### Example
|
|
|
|
/// ```rust,ignore
|
|
|
|
/// let included_str = include_str!("very_large_file.txt");
|
|
|
|
/// let included_bytes = include_bytes!("very_large_file.txt");
|
|
|
|
/// ```
|
|
|
|
///
|
2022-06-16 10:39:06 -05:00
|
|
|
/// Use instead:
|
2022-05-05 09:12:52 -05:00
|
|
|
/// ```rust,ignore
|
|
|
|
/// use std::fs;
|
|
|
|
///
|
2022-06-16 10:39:06 -05:00
|
|
|
/// // You can load the file at runtime
|
2022-05-05 09:12:52 -05:00
|
|
|
/// 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 {
|
2024-07-11 23:00:03 -05:00
|
|
|
pub fn new(conf: &'static Conf) -> Self {
|
|
|
|
Self {
|
|
|
|
max_file_size: conf.max_include_file_size,
|
|
|
|
}
|
2022-05-05 09:12:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_lint_pass!(LargeIncludeFile => [LARGE_INCLUDE_FILE]);
|
|
|
|
|
|
|
|
impl LateLintPass<'_> for LargeIncludeFile {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &'_ Expr<'_>) {
|
2024-06-12 21:46:27 -05:00
|
|
|
if let ExprKind::Lit(lit) = &expr.kind
|
|
|
|
&& let len = match &lit.node {
|
2023-11-16 12:13:24 -06:00
|
|
|
// include_bytes
|
|
|
|
LitKind::ByteStr(bstr, _) => bstr.len(),
|
|
|
|
// include_str
|
|
|
|
LitKind::Str(sym, _) => sym.as_str().len(),
|
|
|
|
_ => return,
|
2022-05-05 09:12:52 -05:00
|
|
|
}
|
2024-06-12 21:46:27 -05:00
|
|
|
&& len as u64 > self.max_file_size
|
|
|
|
&& let Some(macro_call) = root_macro_call_first_node(cx, expr)
|
|
|
|
&& (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))
|
|
|
|
{
|
2023-11-16 12:13:24 -06:00
|
|
|
span_lint_and_note(
|
|
|
|
cx,
|
|
|
|
LARGE_INCLUDE_FILE,
|
2024-03-29 15:34:01 -05:00
|
|
|
expr.span.source_callsite(),
|
2023-11-16 12:13:24 -06:00
|
|
|
"attempted to include a large file",
|
|
|
|
None,
|
2024-04-04 12:52:55 -05:00
|
|
|
format!(
|
2023-11-16 12:13:24 -06:00
|
|
|
"the configuration allows a maximum size of {} bytes",
|
|
|
|
self.max_file_size
|
|
|
|
),
|
|
|
|
);
|
2022-05-05 09:12:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|