Skip to content

[javascript] UnnecessaryBlock FP for try/catch #4055

@dah-fari7009

Description

@dah-fari7009

Affects PMD Version:
6.47.0

Rule:
UnnecessaryBlock
https://pmd.github.io/latest/pmd_rules_ecmascript_codestyle.html#unnecessaryblock

Description:
Same problem as #4054
PMD reports an unnecessary block for the try/catch, but there's no reason why this should be the case. Further, looking into the generate AST by PMD, it seems to be an issue with handling async/await keywords. In fact, without:
image

and with it, it shows EmptyStatement instead:
image

Code Sample demonstrating the issue:

async function run(){
    try{
        await client.connect()
        console.log("Successfully connected to the database")
        var server = app.listen(8081, function (){
            var host = server.address().hostname
            var port = server.address().port
            console.log("Example app running at https://%s:%s", host, port)
        })
    }
    catch(err){
        console.log(err)
        await client.close()

    }
}

Expected outcome:

PMD reports a violation at line 2 for the try block, but that seems to be a false positive.

Running PMD through: [Designer]

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-positivePMD flags a piece of code that is not problematic

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions