diktat icon indicating copy to clipboard operation
diktat copied to clipboard

False positive finding for rule [FILE_UNORDERED_IMPORTS]

Open WebTiger89 opened this issue 3 years ago • 7 comments

[FILE_UNORDERED_IMPORTS] imports should be ordered alphabetically and shouldn't be separated by newlines: import android.bluetooth.BluetoothAdapter...

This is what Android Studio expects (right click on file --> Optimize Imports) and diktatCheck complains:

import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import android.bluetooth.BluetoothManager
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Context.BLUETOOTH_SERVICE
import android.content.Intent
import android.content.IntentFilter
import android.content.pm.PackageManager
import android.util.Log
import de.example.common.jvm.util.whenNotNull
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.flowOn
import java.lang.Short.MIN_VALUE

This is what diktatFix is doing:

import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import android.bluetooth.BluetoothManager
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Context.BLUETOOTH_SERVICE
import android.content.Intent
import android.content.IntentFilter
import android.content.pm.PackageManager
import android.util.Log

import de.example.common.jvm.util.whenNotNull

import java.lang.Short.MIN_VALUE

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.flowOn

WebTiger89 avatar May 28 '22 19:05 WebTiger89

ok sorry, after checking the convention guide I noticed this is the correct behavior of diktat.

I have these grouping never seen before. No android source code out there does this grouping with separated lines. I'm just wondering.

BUT... This is not the format Android Studio expects when it optimizes the imports (right click on file --> Optimize Imports). Any thoughts about that?

AND... Why does diktatCheck not find and fix those imports?:

...
import androidx.compose.ui.unit.sp
import de.example.common.android.compose.theme.MyTheme

It does not find it but according to coding guide it should find it and suggest a new line.

WebTiger89 avatar May 28 '22 20:05 WebTiger89

ok sorry, after checking the convention guide I noticed this is the correct behavior of diktat.

yeah, android development guides sometimes differ from backend/kotlinJS development.

This inspection was suggested to us by two very big international companies (one from Asia and the other from Europe). They are using such convention

orchestr7 avatar May 28 '22 20:05 orchestr7

Any ideas why it does not warn about the last example?

WebTiger89 avatar May 28 '22 20:05 WebTiger89

Any ideas why it does not warn about the last example?

Looks like we haven’t added androidx to the inspection and the code style. It goes to other and is grouped with remaining imports

orchestr7 avatar May 28 '22 21:05 orchestr7

@petertrr could please also check why we ignore org.cqfn.diktat in AstNodeUtils.kt and group them together?

orchestr7 avatar May 30 '22 09:05 orchestr7

We also have a flag to disable this grouping, since it's indeed sometimes a controversial style: https://github.com/saveourtool/diktat/blob/9e3b5cb7d1a84e103ad5020bed096279ad373eb1/diktat-rules/src/main/resources/diktat-analysis.yml#L160-L162 Setting useRecommendedImportsOrder to false will only keep alphabetical ordering without grouping

petertrr avatar May 30 '22 13:05 petertrr

Good to know. Thanks @petertrr :)

WebTiger89 avatar Jun 02 '22 16:06 WebTiger89

@akuleshov7 I guess it can be closed

nulls avatar Dec 19 '23 12:12 nulls