Skip to content
Merged

Rustup #11469

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion .github/driver.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ unset CARGO_MANIFEST_DIR
# Run a lint and make sure it produces the expected output. It's also expected to exit with code 1
# FIXME: How to match the clippy invocation in compile-test.rs?
./target/debug/clippy-driver -Dwarnings -Aunused -Zui-testing --emit metadata --crate-type bin tests/ui/double_neg.rs 2>double_neg.stderr && exit 1
sed -e "s,tests/ui,\$DIR," -e "/= help/d" double_neg.stderr >normalized.stderr
sed -e "s,tests/ui,\$DIR," -e "/= help: for/d" double_neg.stderr > normalized.stderr
diff -u normalized.stderr tests/ui/double_neg.stderr

# make sure "clippy-driver --rustc --arg" and "rustc --arg" behave the same
Expand Down
46 changes: 24 additions & 22 deletions clippy_lints/src/operators/arithmetic_side_effects.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::ARITHMETIC_SIDE_EFFECTS;
use clippy_utils::consts::{constant, constant_simple, Constant};
use clippy_utils::diagnostics::span_lint;
use clippy_utils::ty::{match_type, type_diagnostic_name};
use clippy_utils::ty::type_diagnostic_name;
use clippy_utils::{expr_or_init, is_from_proc_macro, is_lint_allowed, peel_hir_expr_refs, peel_hir_expr_unary};
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_lint::{LateContext, LateLintPass};
Expand All @@ -14,7 +14,12 @@ use {rustc_ast as ast, rustc_hir as hir};

const HARD_CODED_ALLOWED_BINARY: &[[&str; 2]] = &[["f32", "f32"], ["f64", "f64"], ["std::string::String", "str"]];
const HARD_CODED_ALLOWED_UNARY: &[&str] = &["f32", "f64", "std::num::Saturating", "std::num::Wrapping"];
const INTEGER_METHODS: &[&str] = &["saturating_div", "wrapping_div", "wrapping_rem", "wrapping_rem_euclid"];
const INTEGER_METHODS: &[Symbol] = &[
sym::saturating_div,
sym::wrapping_div,
sym::wrapping_rem,
sym::wrapping_rem_euclid,
];

#[derive(Debug)]
pub struct ArithmeticSideEffects {
Expand Down Expand Up @@ -49,7 +54,7 @@ impl ArithmeticSideEffects {
allowed_unary,
const_span: None,
expr_span: None,
integer_methods: INTEGER_METHODS.iter().map(|el| Symbol::intern(el)).collect(),
integer_methods: INTEGER_METHODS.iter().copied().collect(),
}
}

Expand Down Expand Up @@ -89,38 +94,35 @@ impl ArithmeticSideEffects {
op: &Spanned<hir::BinOpKind>,
rhs_ty: Ty<'_>,
) -> bool {
const SATURATING: &[&str] = &["core", "num", "saturating", "Saturating"];
const WRAPPING: &[&str] = &["core", "num", "wrapping", "Wrapping"];
let is_non_zero = |symbol: Option<Symbol>| {
let is_div_or_rem = matches!(op.node, hir::BinOpKind::Div | hir::BinOpKind::Rem);
let is_non_zero_u = |symbol: Option<Symbol>| {
matches!(
symbol,
Some(
sym::NonZeroI128
| sym::NonZeroI16
| sym::NonZeroI32
| sym::NonZeroI64
| sym::NonZeroI8
| sym::NonZeroU128
sym::NonZeroU128
| sym::NonZeroU16
| sym::NonZeroU32
| sym::NonZeroU64
| sym::NonZeroU8
| sym::NonZeroUsize
)
)
};
// If the RHS is NonZero*, then division or module by zero will never occur
if is_non_zero(type_diagnostic_name(cx, rhs_ty)) && let hir::BinOpKind::Div | hir::BinOpKind::Rem = op.node {
return true;
}
// For `Saturation` or `Wrapping` (RHS), all but division and module are allowed.
let is_div_or_rem = matches!(op.node, hir::BinOpKind::Div | hir::BinOpKind::Rem);
if (match_type(cx, rhs_ty, SATURATING) || match_type(cx, rhs_ty, WRAPPING)) && !is_div_or_rem {
let is_sat_or_wrap = |ty: Ty<'_>| {
let is_sat = type_diagnostic_name(cx, ty) == Some(sym::Saturating);
let is_wrap = type_diagnostic_name(cx, ty) == Some(sym::Wrapping);
is_sat || is_wrap
};

// If the RHS is NonZeroU*, then division or module by zero will never occur
if is_non_zero_u(type_diagnostic_name(cx, rhs_ty)) && is_div_or_rem {
return true;
}
// For `Saturation` or `Wrapping` (LHS), everything is allowed
if match_type(cx, lhs_ty, SATURATING) || match_type(cx, lhs_ty, WRAPPING) {
return true;
// `Saturation` and `Wrapping` can overflow if the RHS is zero in a division or module
if is_sat_or_wrap(lhs_ty) {
return !is_div_or_rem;
}

false
}

Expand Down
26 changes: 11 additions & 15 deletions clippy_lints/src/undocumented_unsafe_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use rustc_lexer::{tokenize, TokenKind};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::lint::in_external_macro;
use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::{BytePos, Pos, Span, SyntaxContext};
use rustc_span::{BytePos, Pos, RelativeBytePos, Span, SyntaxContext};

declare_clippy_lint! {
/// ### What it does
Expand Down Expand Up @@ -490,7 +490,7 @@ fn item_has_safety_comment(cx: &LateContext<'_>, item: &hir::Item<'_>) -> HasSaf
match text_has_safety_comment(
src,
&lines[comment_start_line.line + 1..=unsafe_line.line],
unsafe_line.sf.start_pos.to_usize(),
unsafe_line.sf.start_pos,
) {
Some(b) => HasSafetyComment::Yes(b),
None => HasSafetyComment::No,
Expand Down Expand Up @@ -534,7 +534,7 @@ fn stmt_has_safety_comment(cx: &LateContext<'_>, span: Span, hir_id: HirId) -> H
match text_has_safety_comment(
src,
&lines[comment_start_line.line + 1..=unsafe_line.line],
unsafe_line.sf.start_pos.to_usize(),
unsafe_line.sf.start_pos,
) {
Some(b) => HasSafetyComment::Yes(b),
None => HasSafetyComment::No,
Expand Down Expand Up @@ -595,7 +595,7 @@ fn span_from_macro_expansion_has_safety_comment(cx: &LateContext<'_>, span: Span
match text_has_safety_comment(
src,
&lines[macro_line.line + 1..=unsafe_line.line],
unsafe_line.sf.start_pos.to_usize(),
unsafe_line.sf.start_pos,
) {
Some(b) => HasSafetyComment::Yes(b),
None => HasSafetyComment::No,
Expand Down Expand Up @@ -658,7 +658,7 @@ fn span_has_safety_comment(cx: &LateContext<'_>, span: Span) -> bool {
body_line.line < unsafe_line.line && text_has_safety_comment(
src,
&lines[body_line.line + 1..=unsafe_line.line],
unsafe_line.sf.start_pos.to_usize(),
unsafe_line.sf.start_pos,
).is_some()
})
} else {
Expand All @@ -671,13 +671,13 @@ fn span_has_safety_comment(cx: &LateContext<'_>, span: Span) -> bool {
}

/// Checks if the given text has a safety comment for the immediately proceeding line.
fn text_has_safety_comment(src: &str, line_starts: &[BytePos], offset: usize) -> Option<BytePos> {
fn text_has_safety_comment(src: &str, line_starts: &[RelativeBytePos], start_pos: BytePos) -> Option<BytePos> {
let mut lines = line_starts
.array_windows::<2>()
.rev()
.map_while(|[start, end]| {
let start = start.to_usize() - offset;
let end = end.to_usize() - offset;
let start = start.to_usize();
let end = end.to_usize();
let text = src.get(start..end)?;
let trimmed = text.trim_start();
Some((start + (text.len() - trimmed.len()), trimmed))
Expand All @@ -692,9 +692,7 @@ fn text_has_safety_comment(src: &str, line_starts: &[BytePos], offset: usize) ->
let (mut line, mut line_start) = (line, line_start);
loop {
if line.to_ascii_uppercase().contains("SAFETY:") {
return Some(BytePos(
u32::try_from(line_start).unwrap() + u32::try_from(offset).unwrap(),
));
return Some(start_pos + BytePos(u32::try_from(line_start).unwrap()));
}
match lines.next() {
Some((s, x)) if x.starts_with("//") => (line, line_start) = (x, s),
Expand All @@ -707,15 +705,13 @@ fn text_has_safety_comment(src: &str, line_starts: &[BytePos], offset: usize) ->
let (mut line_start, mut line) = (line_start, line);
loop {
if line.starts_with("/*") {
let src = &src[line_start..line_starts.last().unwrap().to_usize() - offset];
let src = &src[line_start..line_starts.last().unwrap().to_usize()];
let mut tokens = tokenize(src);
return (src[..tokens.next().unwrap().len as usize]
.to_ascii_uppercase()
.contains("SAFETY:")
&& tokens.all(|t| t.kind == TokenKind::Whitespace))
.then_some(BytePos(
u32::try_from(line_start).unwrap() + u32::try_from(offset).unwrap(),
));
.then_some(start_pos + BytePos(u32::try_from(line_start).unwrap()));
}
match lines.next() {
Some(x) => (line_start, line) = x,
Expand Down
2 changes: 1 addition & 1 deletion clippy_utils/src/qualify_min_const_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ fn check_terminator<'tcx>(
| TerminatorKind::Goto { .. }
| TerminatorKind::Return
| TerminatorKind::UnwindResume
| TerminatorKind::UnwindTerminate
| TerminatorKind::UnwindTerminate(_)
| TerminatorKind::Unreachable => Ok(()),
TerminatorKind::Drop { place, .. } => {
if !is_ty_const_destruct(tcx, place.ty(&body.local_decls, tcx).ty, body) {
Expand Down
8 changes: 4 additions & 4 deletions clippy_utils/src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use rustc_hir::{BlockCheckMode, Expr, ExprKind, UnsafeSource};
use rustc_lint::{LateContext, LintContext};
use rustc_session::Session;
use rustc_span::source_map::{original_sp, SourceMap};
use rustc_span::{hygiene, BytePos, Pos, SourceFile, Span, SpanData, SyntaxContext, DUMMY_SP};
use rustc_span::{hygiene, BytePos, Pos, SourceFile, SourceFileAndLine, Span, SpanData, SyntaxContext, DUMMY_SP};
use std::borrow::Cow;
use std::ops::Range;

Expand Down Expand Up @@ -117,9 +117,9 @@ fn first_char_in_first_line<T: LintContext>(cx: &T, span: Span) -> Option<BytePo
/// ```
fn line_span<T: LintContext>(cx: &T, span: Span) -> Span {
let span = original_sp(span, DUMMY_SP);
let source_map_and_line = cx.sess().source_map().lookup_line(span.lo()).unwrap();
let line_no = source_map_and_line.line;
let line_start = source_map_and_line.sf.lines(|lines| lines[line_no]);
let SourceFileAndLine { sf, line } = cx.sess().source_map().lookup_line(span.lo()).unwrap();
let line_start = sf.lines(|lines| lines[line]);
let line_start = sf.absolute_position(line_start);
span.with_lo(line_start)
}

Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[toolchain]
channel = "nightly-2023-08-24"
channel = "nightly-2023-09-07"
components = ["cargo", "llvm-tools", "rust-src", "rust-std", "rustc", "rustc-dev", "rustfmt"]
1 change: 1 addition & 0 deletions tests/ui-cargo/cargo_common_metadata/fail/Cargo.stderr
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
error: package `cargo_common_metadata_fail` is missing `package.description` metadata
|
= note: `-D clippy::cargo-common-metadata` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::cargo_common_metadata)]`

error: package `cargo_common_metadata_fail` is missing `either package.license or package.license_file` metadata

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
error: package `cargo_common_metadata_fail_publish` is missing `package.description` metadata
|
= note: `-D clippy::cargo-common-metadata` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::cargo_common_metadata)]`

error: package `cargo_common_metadata_fail_publish` is missing `either package.license or package.license_file` metadata

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
error: package `cargo_common_metadata_fail_publish_true` is missing `package.description` metadata
|
= note: `-D clippy::cargo-common-metadata` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::cargo_common_metadata)]`

error: package `cargo_common_metadata_fail_publish_true` is missing `either package.license or package.license_file` metadata

Expand Down
1 change: 1 addition & 0 deletions tests/ui-cargo/duplicate_mod/fail/Cargo.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ error: file is loaded as a module multiple times: `src/b.rs`
|
= help: replace all but one `mod` item with `use` items
= note: `-D clippy::duplicate-mod` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::duplicate_mod)]`

error: file is loaded as a module multiple times: `src/c.rs`
--> src/main.rs:9:1
Expand Down
2 changes: 2 additions & 0 deletions tests/ui-cargo/feature_name/fail/Cargo.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ error: the "no-" prefix in the feature name "no-qaq" is negative
|
= help: consider renaming the feature to "qaq", but make sure the feature adds functionality
= note: `-D clippy::negative-feature-names` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::negative_feature_names)]`

error: the "no_" prefix in the feature name "no_qaq" is negative
|
Expand All @@ -19,6 +20,7 @@ error: the "-support" suffix in the feature name "qvq-support" is redundant
|
= help: consider renaming the feature to "qvq"
= note: `-D clippy::redundant-feature-names` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::redundant_feature_names)]`

error: the "_support" suffix in the feature name "qvq_support" is redundant
|
Expand Down
1 change: 1 addition & 0 deletions tests/ui-cargo/module_style/fail_mod/Cargo.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ error: `mod.rs` files are required, found `src/bad/inner.rs`
|
= help: move `src/bad/inner.rs` to `src/bad/inner/mod.rs`
= note: `-D clippy::self-named-module-files` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::self_named_module_files)]`

error: `mod.rs` files are required, found `src/bad/inner/stuff.rs`
--> src/bad/inner/stuff.rs:1:1
Expand Down
1 change: 1 addition & 0 deletions tests/ui-cargo/module_style/fail_mod_remap/Cargo.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ error: `mod.rs` files are required, found `src/bad.rs`
|
= help: move `src/bad.rs` to `src/bad/mod.rs`
= note: `-D clippy::self-named-module-files` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::self_named_module_files)]`

error: could not compile `fail-mod-remap` (bin "fail-mod-remap") due to previous error
1 change: 1 addition & 0 deletions tests/ui-cargo/module_style/fail_no_mod/Cargo.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ error: `mod.rs` files are not allowed, found `src/bad/mod.rs`
|
= help: move `src/bad/mod.rs` to `src/bad.rs`
= note: `-D clippy::mod-module-files` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::mod_module_files)]`

error: could not compile `fail-no-mod` (bin "fail-no-mod") due to previous error
1 change: 1 addition & 0 deletions tests/ui-cargo/multiple_crate_versions/fail/Cargo.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
error: multiple versions for dependency `winapi`: 0.2.8, 0.3.9
|
= note: `-D clippy::multiple-crate-versions` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::multiple_crate_versions)]`

error: could not compile `multiple_crate_versions` (bin "multiple_crate_versions") due to previous error
1 change: 1 addition & 0 deletions tests/ui-cargo/wildcard_dependencies/fail/Cargo.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
error: wildcard dependency for `regex`
|
= note: `-D clippy::wildcard-dependencies` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::wildcard_dependencies)]`

error: could not compile `wildcard_dependencies` (bin "wildcard_dependencies") due to previous error
1 change: 1 addition & 0 deletions tests/ui-internal/check_formulation.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ LL | /// Check for lint formulations that are correct
|
= help: try using `Checks for` instead
= note: `-D clippy::almost-standard-lint-formulation` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::almost_standard_lint_formulation)]`

error: non-standard lint formulation
--> $DIR/check_formulation.rs:33:5
Expand Down
1 change: 1 addition & 0 deletions tests/ui-internal/if_chain_style.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ help: this `let` statement can also be in the `if_chain!`
LL | let x = "";
| ^^^^^^^^^^^
= note: `-D clippy::if-chain-style` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::if_chain_style)]`

error: `if a && b;` should be `if a; if b;`
--> $DIR/if_chain_style.rs:24:12
Expand Down
1 change: 1 addition & 0 deletions tests/ui-internal/invalid_paths.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ LL | pub const TRANSMUTE: [&str; 4] = ["core", "intrinsics", "", "transmute"
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::invalid-paths` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::invalid_paths)]`

error: invalid path
--> $DIR/invalid_paths.rs:18:5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ LL | const DEREF_TRAIT: [&str; 4] = ["core", "ops", "deref", "Deref"];
|
= help: convert all references to use `sym::Deref`
= note: `-D clippy::unnecessary-def-path` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::unnecessary_def_path)]`

error: hardcoded path to a language item
--> $DIR/unnecessary_def_path_hardcoded_path.rs:11:40
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ LL | std::f32::MAX;
| ^^^^^^^^^^^^^
|
= note: `-D clippy::absolute-paths` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::absolute_paths)]`

error: consider bringing this path into scope with the `use` keyword
--> $DIR/absolute_paths.rs:41:5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ LL | std::f32::MAX;
| ^^^^^^^^^^^^^
|
= note: `-D clippy::absolute-paths` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::absolute_paths)]`

error: consider bringing this path into scope with the `use` keyword
--> $DIR/absolute_paths.rs:41:5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ LL | println!("val='{}'", local_i32);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::uninlined-format-args` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::uninlined_format_args)]`
help: change this to
|
LL - println!("val='{}'", local_i32);
Expand All @@ -30,6 +31,7 @@ LL | println!("Hello {} is {:.*}", "x", local_i32, local_f64);
| ^^^
|
= note: `-D clippy::print-literal` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::print_literal)]`
help: try
|
LL - println!("Hello {} is {:.*}", "x", local_i32, local_f64);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ LL | let _ = Baz + Baz;
| ^^^^^^^^^
|
= note: `-D clippy::arithmetic-side-effects` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::arithmetic_side_effects)]`

error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects_allowed.rs:80:13
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ LL | const ABOVE: [u8; 11] = [0; 11];
| help: make this a static item: `static`
|
= note: `-D clippy::large-const-arrays` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::large_const_arrays)]`

error: allocating a local array larger than 10 bytes
--> $DIR/array_size_threshold.rs:4:25
Expand All @@ -16,6 +17,7 @@ LL | const ABOVE: [u8; 11] = [0; 11];
|
= help: consider allocating on the heap with `vec![0; 11].into_boxed_slice()`
= note: `-D clippy::large-stack-arrays` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::large_stack_arrays)]`

error: allocating a local array larger than 10 bytes
--> $DIR/array_size_threshold.rs:8:17
Expand Down
Loading