Skip to content
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

CanBeNonNullable false-negative #4492

Closed
BraisGabin opened this issue Jan 13, 2022 · 7 comments
Closed

CanBeNonNullable false-negative #4492

BraisGabin opened this issue Jan 13, 2022 · 7 comments
Assignees
Labels
false-negative good first issue Issue that is easy to pickup for people that are new to the project help wanted

Comments

@BraisGabin
Copy link
Member

Expected Behavior

assignedExpression violates CanBeNonNullable.

private fun visitAssignment(assignedExpression: KtExpression?) {
    if (assignedExpression == null) return
    val name = if (assignedExpression is KtQualifiedExpression) {
        assignedExpression.selectorExpression
    } else {
        assignedExpression
    }?.text ?: return
    assignments.getOrPut(name) { mutableSetOf() }.add(assignedExpression)
}

Observed Behavior

detekt is fine with that code

Context

If a function returns Unit any null check to exit the function if the parameter is null should be consider as a parameter that could be non-null.

I saw this false-negative while working on this PR: #4491

Your Environment

  • Version of detekt used: main
  • Link to your project (if it's a public repository): detekt.
@cortinico
Copy link
Member

Isn't #4431 supposed to catch this actually?

@BraisGabin
Copy link
Member Author

Yes, but it is not.

@cortinico cortinico added good first issue Issue that is easy to pickup for people that are new to the project help wanted labels Feb 1, 2022
@VitalyVPinchuk
Copy link
Contributor

Hi!
Can I work on this?

@BraisGabin
Copy link
Member Author

Please go ahead :)

@VitalyVPinchuk
Copy link
Contributor

Should !is in if / when statements also be reported?
e.g.

fun foo(a: Int?) {
    if (a !is Int) return
    println(a)
}

@BraisGabin
Copy link
Member Author

Never though of someone writing something like that. But if it's not too difficult to control that case I don't see why not.

@VitalyVPinchuk
Copy link
Contributor

I'm just looking through the tests for the rule and there are such cases
Yes, I'd rather stick to more common ways of null checking. At least for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
false-negative good first issue Issue that is easy to pickup for people that are new to the project help wanted
Projects
None yet
Development

No branches or pull requests

3 participants