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

false positive finding for WRONG_WHITESPACE on gradle plugins block #1313

Closed
WebTiger89 opened this issue May 27, 2022 · 8 comments · Fixed by #1354
Closed

false positive finding for WRONG_WHITESPACE on gradle plugins block #1313

WebTiger89 opened this issue May 27, 2022 · 8 comments · Fixed by #1354
Assignees
Labels
bug Something isn't working

Comments

@WebTiger89
Copy link

WebTiger89 commented May 27, 2022

The warning states:

[WRONG_WHITESPACE] incorrect usage of whitespaces for code separation: version should have 1 space(s) before and 1 space(s) after, but has 0 space(s) after

for code in settings.gradle.kts:

plugins {
    id("com.gradle.enterprise") version("3.10.1") // this line
}

Maybe the rule can be adjusted/improved here.

@WebTiger89 WebTiger89 added the bug Something isn't working label May 27, 2022
@orchestr7
Copy link
Member

orchestr7 commented May 27, 2022

That looks like a bug actually, @WebTiger89 you can exclude .kts scripts while we are investigating it:

 configure<DiktatExtension> {
        diktatConfigFile = rootProject.file("diktat-analysis.yml")
        githubActions = findProperty("diktat.githubActions")?.toString()?.toBoolean() ?: false
        inputs {
           exclude("**/*.kt")
        }
    }

@orchestr7 orchestr7 assigned petertrr and unassigned petertrr May 27, 2022
@orchestr7
Copy link
Member

orchestr7 commented May 27, 2022

Also this issue affects comments. Very critical, should be fixed asap

[WRONG_WHITESPACE] incorrect usage of whitespaces for code separation: version should have 1 space(s) before and 1 space(s) after, but has 0 space(s) after
[COMMENT_WHITE_SPACE] there should be a white space between code and comment also between code start token and comment text: There should be 2 space(s) before comment text, but there are too many in // this line
@Test
@Tag(WarningNames.WRONG_WHITESPACE)
fun `regression related to gradle plugin with versions`() {
    lintMethod(
        """
            |plugins {
            |   id("com.gradle.enterprise") version("3.10.1") // this line
            |}
        """.trimMargin()
    )
}

@WebTiger89
Copy link
Author

Thank you very much. I have found a lot of false positives. I will open new issues for them now. I hope this won't be too annoying ^^

@orchestr7
Copy link
Member

orchestr7 commented May 27, 2022

Thank you very much. I have found a lot of false positives. I will open new issues for them now. I hope this won't be too annoying ^^

That would be extremely useful! You cannot even imagine, how we appreciate that.
As we cannot imagine all language scenarios...

@WebTiger89
Copy link
Author

Here is another example:

    val onClick: () -> Unit = remember {
        {
            when {
                ContextCompat.checkSelfPermission(
                    context,
                    permission
                ) == PackageManager.PERMISSION_GRANTED -> {
                    // You can use the API that requires the permission.
                    Log.d("PermissionComponent", "permission [$permission].")
                    onPermissionGranted()
                }
                ActivityCompat.shouldShowRequestPermissionRationale(
                    context as Activity,
                    permission
                ) -> {
                    /*
                     * In an educational UI, explain to the user why your app requires this
                     * permission for a specific feature to behave as expected. In this UI,
                     * include a "cancel" or "no thanks" button that allows the user to
                     * continue using your app without granting the permission.
                     */
                    Log.d("PermissionComponent", "show rationale.")
                    permissionState.showRationale = true
                }
                else -> {
                    // Asking for permission
                    Log.d("PermissionComponent", "ask for permission [$permission].")
                    requestPermissionLauncher.launch(permission)
                }
            }
        }
    }

It is the second line.

[WRONG_WHITESPACE] incorrect usage of whitespaces for code separation: there should be a whitespace before '{'

@petertrr
Copy link
Member

The warning states:

[WRONG_WHITESPACE] incorrect usage of whitespaces for code separation: version should have 1 space(s) before and 1 space(s) after, but has 0 space(s) after

Hey @WebTiger89, actually this finding is not a false positive. version in gradle scripts is an infix function which has two types of call syntax: like a regular function id("").version("") and like an infix function without both dot and parentheses: id("") version "". What you have is a second variant but with additional parentheses around version string, which are not part of the function call, but rather a pair of regular parentheses, which are used for grouping operations inside them. So in the AST representation the whole expression ("3.10.1") is an argument of version function, and diktat suggests to insert a white space after the infix function call.

@WebTiger89
Copy link
Author

Thanks for the details. What about the second example?

@petertrr
Copy link
Member

Yes, the second example is a bug in diktat. I think we usually expect opening braces to be on the same line as the previous code and therefore don't properly handle cases when a line starting with white spaces and an opening braces is indeed valid.

petertrr added a commit that referenced this issue Jun 30, 2022
…1354)

### What's done:
* Fixed logic
* Added test

This pull request closes #1313
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants