Skip to content

Commit

Permalink
Dogfood missing_assert_message on Clippy
Browse files Browse the repository at this point in the history
Co-authored-by: Weihang Lo <me@weihanglo.tw>
  • Loading branch information
unexge and weihanglo committed Feb 16, 2023
1 parent f9e133a commit ec65357
Show file tree
Hide file tree
Showing 14 changed files with 36 additions and 20 deletions.
2 changes: 1 addition & 1 deletion clippy_lints/src/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ pub fn strip_doc_comment_decoration(doc: &str, comment_kind: CommentKind, span:
let mut contains_initial_stars = false;
for line in doc.lines() {
let offset = line.as_ptr() as usize - doc.as_ptr() as usize;
debug_assert_eq!(offset as u32 as usize, offset);
debug_assert_eq!(offset as u32 as usize, offset, "`offset` shouldn't overflow `u32`");
contains_initial_stars |= line.trim_start().starts_with('*');
// +1 adds the newline, +3 skips the opening delimiter
sizes.push((line.len() + 1, span.with_lo(span.lo() + BytePos(3 + offset as u32))));
Expand Down
6 changes: 5 additions & 1 deletion clippy_lints/src/duplicate_mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ impl EarlyLintPass for DuplicateMod {
}

// At this point the lint would be emitted
assert_eq!(spans.len(), lint_levels.len());
assert_eq!(
spans.len(),
lint_levels.len(),
"`spans` and `lint_levels` should have equal lengths"
);
let spans: Vec<_> = spans
.iter()
.zip(lint_levels)
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/enum_variants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ fn to_camel_case(item_name: &str) -> String {
impl LateLintPass<'_> for EnumVariantNames {
fn check_item_post(&mut self, _cx: &LateContext<'_>, _item: &Item<'_>) {
let last = self.modules.pop();
assert!(last.is_some());
assert!(last.is_some(), "`modules` should not be empty");
}

#[expect(clippy::similar_names)]
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
store.register_late_pass(|_| Box::new(size_of_ref::SizeOfRef));
store.register_late_pass(|_| Box::new(multiple_unsafe_ops_per_block::MultipleUnsafeOpsPerBlock));
store.register_late_pass(|_| Box::new(extra_unused_type_parameters::ExtraUnusedTypeParameters));
store.register_pre_expansion_pass(|| Box::new(missing_assert_message::MissingAssertMessage));
store.register_pre_expansion_pass(|| Box::<missing_assert_message::MissingAssertMessage>::default());
// add lints here, do not remove this comment, it's used in `new_lint`
}

Expand Down
5 changes: 4 additions & 1 deletion clippy_lints/src/non_expressive_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,10 @@ fn do_check(lint: &mut NonExpressiveNames, cx: &EarlyContext<'_>, attrs: &[Attri
/// Precondition: `a_name.chars().count() < b_name.chars().count()`.
#[must_use]
fn levenstein_not_1(a_name: &str, b_name: &str) -> bool {
debug_assert!(a_name.chars().count() < b_name.chars().count());
debug_assert!(
a_name.chars().count() < b_name.chars().count(),
"Precondition: `a_name.chars().count() < b_name.chars().count()` does not meet"
);
let mut a_chars = a_name.chars();
let mut b_chars = b_name.chars();
while let (Some(a), Some(b)) = (a_chars.next(), b_chars.next()) {
Expand Down
6 changes: 4 additions & 2 deletions clippy_utils/src/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub struct LimitStack {

impl Drop for LimitStack {
fn drop(&mut self) {
assert_eq!(self.stack.len(), 1);
assert_eq!(self.stack.len(), 1, "stack should only have one element");
}
}

Expand All @@ -49,7 +49,9 @@ impl LimitStack {
}
pub fn pop_attrs(&mut self, sess: &Session, attrs: &[ast::Attribute], name: &'static str) {
let stack = &mut self.stack;
parse_attrs(sess, attrs, name, |val| assert_eq!(stack.pop(), Some(val)));
parse_attrs(sess, attrs, name, |val| {
assert_eq!(stack.pop(), Some(val), "incorrect last element");
});
}
}

Expand Down
11 changes: 7 additions & 4 deletions clippy_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1011,10 +1011,13 @@ pub fn capture_local_usage(cx: &LateContext<'_>, e: &Expr<'_>) -> CaptureKind {
capture
}

debug_assert!(matches!(
e.kind,
ExprKind::Path(QPath::Resolved(None, Path { res: Res::Local(_), .. }))
));
debug_assert!(
matches!(
e.kind,
ExprKind::Path(QPath::Resolved(None, Path { res: Res::Local(_), .. }))
),
"`e.kind` should be a resolved local path"
);

let mut child_id = e.hir_id;
let mut capture = CaptureKind::Value;
Expand Down
6 changes: 3 additions & 3 deletions clippy_utils/src/numeric_literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ impl<'a> NumericLiteral<'a> {
}

pub fn group_digits(output: &mut String, input: &str, group_size: usize, partial_group_first: bool, pad: bool) {
debug_assert!(group_size > 0);
debug_assert!(group_size > 0, "group size should be greater than zero");

let mut digits = input.chars().filter(|&c| c != '_');

Expand Down Expand Up @@ -219,7 +219,7 @@ impl<'a> NumericLiteral<'a> {
}

fn split_suffix<'a>(src: &'a str, lit_kind: &LitKind) -> (&'a str, Option<&'a str>) {
debug_assert!(lit_kind.is_numeric());
debug_assert!(lit_kind.is_numeric(), "`lit_kind` should be numeric");
lit_suffix_length(lit_kind)
.and_then(|suffix_length| src.len().checked_sub(suffix_length))
.map_or((src, None), |split_pos| {
Expand All @@ -229,7 +229,7 @@ fn split_suffix<'a>(src: &'a str, lit_kind: &LitKind) -> (&'a str, Option<&'a st
}

fn lit_suffix_length(lit_kind: &LitKind) -> Option<usize> {
debug_assert!(lit_kind.is_numeric());
debug_assert!(lit_kind.is_numeric(), "`lit_kind` should be numeric");
let suffix = match lit_kind {
LitKind::Int(_, int_lit_kind) => match int_lit_kind {
LitIntType::Signed(int_ty) => Some(int_ty.name_str()),
Expand Down
3 changes: 1 addition & 2 deletions clippy_utils/src/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,7 @@ pub fn implements_trait_with_env<'tcx>(
trait_id: DefId,
ty_params: impl IntoIterator<Item = Option<GenericArg<'tcx>>>,
) -> bool {
// Clippy shouldn't have infer types
assert!(!ty.needs_infer());
assert!(!ty.needs_infer(), "Clippy shouldn't have infer types");

let ty = tcx.erase_regions(ty);
if ty.has_escaping_bound_vars() {
Expand Down
3 changes: 2 additions & 1 deletion lintcheck/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ impl Crate {
.status()
.expect("failed to run cargo");

assert_eq!(status.code(), Some(0));
assert_eq!(status.code(), Some(0), "`cargo check` exited with non-zero code");

return Vec::new();
}
Expand Down Expand Up @@ -741,6 +741,7 @@ fn print_stats(old_stats: HashMap<String, usize>, new_stats: HashMap<&String, us
let mut new_stats_deduped = new_stats;

// remove duplicates from both hashmaps
#[allow(clippy::missing_assert_message)]
for (k, v) in &same_in_both_hashmaps {
assert!(old_stats_deduped.remove(k) == Some(*v));
assert!(new_stats_deduped.remove(k) == Some(*v));
Expand Down
5 changes: 4 additions & 1 deletion tests/compile-test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,10 @@ fn check_rustfix_coverage() {
};

if let Ok(missing_coverage_contents) = std::fs::read_to_string(missing_coverage_path) {
assert!(RUSTFIX_COVERAGE_KNOWN_EXCEPTIONS.iter().is_sorted_by_key(Path::new));
assert!(
RUSTFIX_COVERAGE_KNOWN_EXCEPTIONS.iter().is_sorted_by_key(Path::new),
"`RUSTFIX_COVERAGE_KNOWN_EXCEPTIONS` should be sorted"
);

for rs_file in missing_coverage_contents.lines() {
let rs_path = Path::new(rs_file);
Expand Down
2 changes: 1 addition & 1 deletion tests/dogfood.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,5 @@ fn run_clippy_for_package(project: &str, args: &[&str]) {
println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
println!("stderr: {}", String::from_utf8_lossy(&output.stderr));

assert!(output.status.success());
assert!(output.status.success(), "dogfood subcommand failed");
}
1 change: 1 addition & 0 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const CARGO_CLIPPY: &str = "cargo-clippy";
const CARGO_CLIPPY: &str = "cargo-clippy.exe";

#[cfg_attr(feature = "integration", test)]
#[allow(clippy::missing_assert_message)]
fn integration_test() {
let repo_name = env::var("INTEGRATION").expect("`INTEGRATION` var not set");
let repo_url = format!("https://github.com/{repo_name}");
Expand Down
2 changes: 1 addition & 1 deletion tests/test_utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::sync::LazyLock;

pub static CARGO_CLIPPY_PATH: LazyLock<PathBuf> = LazyLock::new(|| {
let mut path = std::env::current_exe().unwrap();
assert!(path.pop()); // deps
assert!(path.pop(), "current running executable path shouldn't be empty"); // deps
path.set_file_name("cargo-clippy");
path
});
Expand Down

0 comments on commit ec65357

Please sign in to comment.