How to suppress lint check for a single file, or can I suppress "wrong" import lint checks?

1,288 views
Skip to first unread message

colto...@protonmail.com

unread,
Jan 17, 2022, 4:34:23 PM1/17/22
to lint-dev
I wrote a lint check to make sure that I don't use the wrong class. In this.001% case, using this "banned" import is actually required.

I can't figure out how to suppress it. So I thought maybe I could just suppress the single file or maybe I can annotate the class with some cool @SuppressClass("my check) or something.

Any thoughts?

Tor Norbye

unread,
Jan 27, 2022, 11:45:31 AM1/27/22
to lint-dev
Your lint check can go to the top level / first class in the compilation unit and look for suppressions there too.
Actually, I think lint should already be doing that automatically on your behalf:

So maybe you just need to try adding your @SuppressWarnings on the top level class? 
(Hopefully our quickfix to suppress lint warnings also puts it there! If not would you mind filing a bug?)

-- Tor

colto...@protonmail.com

unread,
Jan 28, 2022, 12:39:07 AM1/28/22
to lint-dev
So it's not actually a class... it's a file with a bunch of extension functions. Quick fix for "Suppress: Add @SuppressLint("BannedImport") annotation" doesn't actually add anything.

Any other ideas Tor?

colto...@protonmail.com

unread,
Jan 28, 2022, 12:51:32 AM1/28/22
to lint-dev
If it helps... here is my lint check

private class ImportVisitor(private val context: JavaContext) : UElementHandler() {
override fun visitImportStatement(node: UImportStatement) {
if (node.asRenderString().contains("com.dontuse.this.import")) {
val location = context.getLocation(node)
context.report(ISSUE, node, location, EXPLANATION)
}
}
}

but then I actually use this import, in a plain ol' kotlin file (not technically a class) because I use it as an extension function and I don't know how to stop the errors from being reported. lol

Tor Norbye

unread,
Jan 28, 2022, 7:17:26 PM1/28/22
to colto...@protonmail.com, lint-dev
Oh, it's Kotlin. In that case, you could add
@file:Suppress("SuspiciousImport")

package com.example.whatever

at the top of the file (above the package statement).

But if you want to suppress it for a *specific* import statement, you can use the comment support for suppresses, e.g.

import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
//noinspection BannedImport
import android.R;
We should probably fix the quickfix such that it uses the above mechanism if you try to suppress an import statement. Would you mind filing a request for that?

-- Tor


--
You received this message because you are subscribed to the Google Groups "lint-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lint-dev+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lint-dev/e4dab09a-b514-469e-9197-879f4fe18b85n%40googlegroups.com.

colto...@protonmail.com

unread,
Jan 28, 2022, 10:20:50 PM1/28/22
to lint-dev
That worked!!!!! Thank you! Filed a bug here so that quickfix would actually recommend this. 😄
https://issuetracker.google.com/issues/216663026
Reply all
Reply to author
Forward
0 new messages