Skip to content

Add assert_on_const lint#540

Merged
integraledelebesgue merged 1 commit into
mainfrom
feature/assert-on-const
Jun 16, 2026
Merged

Add assert_on_const lint#540
integraledelebesgue merged 1 commit into
mainfrom
feature/assert-on-const

Conversation

@integraledelebesgue

@integraledelebesgue integraledelebesgue commented Jun 9, 2026

Copy link
Copy Markdown
Member

Closes #509

The implementation is identical to #388, with an additional check that fixes the panic:

// `LoweringGroup::lowered_body` can only be applied to functions that have no generic parameters in scope.
if has_generic_params(db, function_with_body_id) {
return None;
}

Additional tests verify the proper behaviour in problematic cases (including the repro from the issue):

/// A trait function with a default body which is not fully concrete (its impl is the abstract `Self` impl)
/// but has no generic parameters, so the lint should handle it.
const BOOL_CONST_IN_TRAIT_FUNCTION: &str = r#"
trait Trait {
fn foo() {
assert!(true, "message");
}
}
"#;
/// A trait function with a default body which is not fully concrete (its impl is the abstract `Self` impl)
/// and additionally pulls an associated type into a body variable.
/// Lint should fail gracefully when trying to retrieve the lowering of the function and not panic.
const BOOL_CONST_IN_TRAIT_FUNCTION_WITH_ASSOCIATED_TYPE: &str = r#"
trait Trait {
type Item;
fn item() -> Self::Item;
fn foo() {
let _item = Self::item();
assert!(true, "message");
}
}
"#;
/// A trait function which is not fully concrete and has a generic parameter - it silently takes a generic impl of PartialEq as a param.
/// Lint should fail gracefully when trying to retrieve the lowering of the function and not panic.
const BOOL_EXPR_NOT_FULLY_CONCRETE_FUNCTION: &str = r#"
pub trait MyTrait<T> {
fn foo<+PartialEq<T>>(a: @T, b: @T);
}
impl MyTraitImpl<T> of MyTrait<T> {
fn foo<+PartialEq<T>>(a: @T, b: @T) {
assert!(a == b, "message");
}
}
"#;

Comment thread src/lints/assert_on_const.rs Outdated

@Arcticae Arcticae left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Next time pls revert the commit + add one on top with the changes - it will make it easier to review 😄

@integraledelebesgue integraledelebesgue added this pull request to the merge queue Jun 16, 2026
Merged via the queue into main with commit 3a5dea0 Jun 16, 2026
3 checks passed
@integraledelebesgue integraledelebesgue deleted the feature/assert-on-const branch June 16, 2026 17:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

assert_on_consts panic

3 participants