-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add large_include_file
lint
#8727
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
), | ||
); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
max-include-file-size = 600 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#![warn(clippy::large_include_file)] | ||
|
||
// Good | ||
const GOOD_INCLUDE_BYTES: &[u8; 581] = include_bytes!("large_include_file.rs"); | ||
const GOOD_INCLUDE_STR: &str = include_str!("large_include_file.rs"); | ||
|
||
#[allow(clippy::large_include_file)] | ||
const ALLOWED_TOO_BIG_INCLUDE_BYTES: &[u8; 654] = include_bytes!("too_big.txt"); | ||
#[allow(clippy::large_include_file)] | ||
const ALLOWED_TOO_BIG_INCLUDE_STR: &str = include_str!("too_big.txt"); | ||
|
||
// Bad | ||
const TOO_BIG_INCLUDE_BYTES: &[u8; 654] = include_bytes!("too_big.txt"); | ||
const TOO_BIG_INCLUDE_STR: &str = include_str!("too_big.txt"); | ||
|
||
Serial-ATA marked this conversation as resolved.
Show resolved
Hide resolved
|
||
fn main() {} |
21 changes: 21 additions & 0 deletions
21
tests/ui-toml/large_include_file/large_include_file.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
error: attempted to include a large file | ||
--> $DIR/large_include_file.rs:13:43 | ||
| | ||
LL | const TOO_BIG_INCLUDE_BYTES: &[u8; 654] = include_bytes!("too_big.txt"); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: `-D clippy::large-include-file` implied by `-D warnings` | ||
= note: the configuration allows a maximum size of 600 bytes | ||
= note: this error originates in the macro `include_bytes` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error: attempted to include a large file | ||
--> $DIR/large_include_file.rs:14:35 | ||
| | ||
LL | const TOO_BIG_INCLUDE_STR: &str = include_str!("too_big.txt"); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: the configuration allows a maximum size of 600 bytes | ||
= note: this error originates in the macro `include_str` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error: aborting due to 2 previous errors | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Maecenas accumsan lacus vel facilisis volutpat. Etiam dignissim diam quis enim lobortis scelerisque fermentum dui faucibus. Tellus id interdum velit laoreet id donec ultrices. Est ultricies integer quis auctor elit sed vulputate. Erat velit scelerisque in dictum non consectetur a erat nam. Sed blandit libero volutpat sed. Tortor condimentum lacinia quis vel eros. Enim ut tellus elementum sagittis vitae et leo duis. Congue mauris rhoncus aenean vel elit scelerisque. Id consectetur purus ut faucibus pulvinar elementum integer. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown field `foobar`, expected one of `avoid-breaking-exported-api`, `msrv`, `blacklisted-names`, `cognitive-complexity-threshold`, `cyclomatic-complexity-threshold`, `doc-valid-idents`, `too-many-arguments-threshold`, `type-complexity-threshold`, `single-char-binding-names-threshold`, `too-large-for-stack`, `enum-variant-name-threshold`, `enum-variant-size-threshold`, `verbose-bit-mask-threshold`, `literal-representation-threshold`, `trivial-copy-size-limit`, `pass-by-value-size-limit`, `too-many-lines-threshold`, `array-size-threshold`, `vec-box-size-threshold`, `max-trait-bounds`, `max-struct-bools`, `max-fn-params-bools`, `warn-on-all-wildcard-imports`, `disallowed-methods`, `disallowed-types`, `unreadable-literal-lint-fractions`, `upper-case-acronyms-aggressive`, `cargo-ignore-publish`, `standard-macro-braces`, `enforced-import-renames`, `allowed-scripts`, `enable-raw-pointer-heuristic-for-send`, `max-suggested-slice-pattern-length`, `await-holding-invalid-types`, `third-party` at line 5 column 1 | ||
error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown field `foobar`, expected one of `avoid-breaking-exported-api`, `msrv`, `blacklisted-names`, `cognitive-complexity-threshold`, `cyclomatic-complexity-threshold`, `doc-valid-idents`, `too-many-arguments-threshold`, `type-complexity-threshold`, `single-char-binding-names-threshold`, `too-large-for-stack`, `enum-variant-name-threshold`, `enum-variant-size-threshold`, `verbose-bit-mask-threshold`, `literal-representation-threshold`, `trivial-copy-size-limit`, `pass-by-value-size-limit`, `too-many-lines-threshold`, `array-size-threshold`, `vec-box-size-threshold`, `max-trait-bounds`, `max-struct-bools`, `max-fn-params-bools`, `warn-on-all-wildcard-imports`, `disallowed-methods`, `disallowed-types`, `unreadable-literal-lint-fractions`, `upper-case-acronyms-aggressive`, `cargo-ignore-publish`, `standard-macro-braces`, `enforced-import-renames`, `allowed-scripts`, `enable-raw-pointer-heuristic-for-send`, `max-suggested-slice-pattern-length`, `await-holding-invalid-types`, `max-include-file-size`, `third-party` at line 5 column 1 | ||
|
||
error: aborting due to previous error | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.