detekt: False positive on `MissingPackageDeclaration` for file in root package

Using 1.22.0 if I enable MissingPackageDeclaration then I get a false positive for a file in the root package (might happen on other versions as well; I just enabled it now).

There’s no way to provide a package declaration for a file in the root package, so I wouldn’t say that the package was “missing”.

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Comments: 25 (24 by maintainers)

Most upvoted comments

As mentioned above all this rule is trying to communicate is that it is not a good idea to have a kotlin source file without a package declaration. IMO the name of the rule carries that meaning quite well.

Could you elaborate on what you are trying to achieve so we can maybe come up with a different solution.

I would vote to rename this rule ForbiddenFileWithoutPackageDeclaration or something similar. The position of the file is not important in this rule. This rule just checks if the package is there or if it’s not there.

Not allowing files in the root package sounds like it should be a separate rule.

That’s basically what the MissingPackageDeclaration does actually

Even if the correct location for a file without package is the root you may want to discourage the files without package. So, on that case, both rules have sense.

How could we improve the documentation of those rules to help make this more clear?