Skip to content

Commit

Permalink
Convert tabs in multiline strings to 4 spaces (rules) (detekt#1961)
Browse files Browse the repository at this point in the history
In the past this repo used tabs instead of 4 spaces for .kt files.
Ktlint doesn't convert tabs in multiline strings to spaces.
Please take a look at the following issue.
pinterest/ktlint#575

Therefore, the following command has been used for conversion.

find ./ -iname '*.kt' -type f -exec bash -c 'expand -t 4 "$0" | sponge "$0"' {} \;

* ./ is recursively searching the given directory
* -iname is a case insensitive match .kt files
* type -f finds only regular files (no directories, binaries or symlinks)
* -exec bash -c executes the following commands in a subshell for each file
* expand -t 4 expands all tabs to 4 spaces
* sponge soaks up standard input (from expand) and writes it to the same file

Source:
https://stackoverflow.com/a/43523362
  • Loading branch information
schalkms authored and Stanislav Myachenkov committed Dec 9, 2019
1 parent be9c7a0 commit 84f8cef
Show file tree
Hide file tree
Showing 73 changed files with 2,562 additions and 2,562 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@ private class UnusedFunctionVisitor(allowedNames: Regex) : UnusedMemberVisitor(a
}

/*
* We need to collect all private function declarations and references to these functions
* for the whole file as Kotlin allows to access private and internal object declarations
* from everywhere in the file.
*/
* We need to collect all private function declarations and references to these functions
* for the whole file as Kotlin allows to access private and internal object declarations
* from everywhere in the file.
*/

override fun visitReferenceExpression(expression: KtReferenceExpression) {
super.visitReferenceExpression(expression)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,24 @@ class DuplicateCaseInWhenExpressionSpec : Spek({

it("reports duplicated label in when") {
val code = """
fun f() {
when (1) {
1 -> println()
1 -> println()
else -> println()
}
}"""
fun f() {
when (1) {
1 -> println()
1 -> println()
else -> println()
}
}"""
assertThat(subject.compileAndLint(code)).hasSize(1)
}

it("does not report duplicated label in when") {
val code = """
fun f() {
when (1) {
1 -> println()
else -> println()
}
}"""
fun f() {
when (1) {
1 -> println()
else -> println()
}
}"""
assertThat(subject.compileAndLint(code)).hasSize(0)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,35 @@ class EqualsWithHashCodeExistSpec : Spek({

it("reports hashCode() without equals() function") {
val code = """
class A {
override fun hashCode(): Int { return super.hashCode() }
}"""
class A {
override fun hashCode(): Int { return super.hashCode() }
}"""
assertThat(subject.compileAndLint(code)).hasSize(1)
}

it("reports equals() without hashCode() function") {
val code = """
class A {
override fun equals(other: Any?): Boolean { return super.equals(other) }
}"""
class A {
override fun equals(other: Any?): Boolean { return super.equals(other) }
}"""
assertThat(subject.compileAndLint(code)).hasSize(1)
}

it("does not report equals() with hashCode() function") {
val code = """
class A {
override fun equals(other: Any?): Boolean { return super.equals(other) }
override fun hashCode(): Int { return super.hashCode() }
}"""
class A {
override fun equals(other: Any?): Boolean { return super.equals(other) }
override fun hashCode(): Int { return super.hashCode() }
}"""
assertThat(subject.compileAndLint(code)).isEmpty()
}

it("does not report when using kotlin.Any?") {
val code = """
class A {
override fun equals(other: kotlin.Any?): Boolean { return super.equals(other) }
override fun hashCode(): Int { return super.hashCode() }
}"""
class A {
override fun equals(other: kotlin.Any?): Boolean { return super.equals(other) }
override fun hashCode(): Int { return super.hashCode() }
}"""
assertThat(subject.compileAndLint(code)).isEmpty()
}
}
Expand All @@ -52,11 +52,11 @@ class EqualsWithHashCodeExistSpec : Spek({

it("does not report equals() or hashcode() violation on data class") {
val code = """
data class EqualsData(val i: Int) {
override fun equals(other: Any?): Boolean {
return super.equals(other)
}
}"""
data class EqualsData(val i: Int) {
override fun equals(other: Any?): Boolean {
return super.equals(other)
}
}"""
assertThat(subject.compileAndLint(code)).hasSize(0)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ class ExplicitGarbageCollectionCallSpec : Spek({

it("reports garbage collector calls") {
val code = """
fun f() {
System.gc()
Runtime.getRuntime().gc()
System.runFinalization()
}"""
fun f() {
System.gc()
Runtime.getRuntime().gc()
System.runFinalization()
}"""
assertThat(subject.compileAndLint(code)).hasSize(3)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,34 @@ class InvalidRangeSpec : Spek({

it("does not report correct bounds in for loop conditions") {
val code = """
fun f() {
for (i in 2..2) {}
for (i in 2 downTo 2) {}
for (i in 2 until 2) {}
for (i in 2 until 4 step 2) {}
for (i in (1+1)..3) { }
}"""
fun f() {
for (i in 2..2) {}
for (i in 2 downTo 2) {}
for (i in 2 until 2) {}
for (i in 2 until 4 step 2) {}
for (i in (1+1)..3) { }
}"""
assertThat(subject.compileAndLint(code)).hasSize(0)
}

it("reports incorrect bounds in for loop conditions") {
val code = """
fun f() {
for (i in 2..1) { }
for (i in 1 downTo 2) { }
for (i in 2 until 1) { }
for (i in 2 until 1 step 2) { }
}"""
fun f() {
for (i in 2..1) { }
for (i in 1 downTo 2) { }
for (i in 2 until 1) { }
for (i in 2 until 1 step 2) { }
}"""
assertThat(subject.compileAndLint(code)).hasSize(4)
}

it("reports nested loops with incorrect bounds in for loop conditions") {
val code = """
fun f() {
for (i in 2..2) {
for (i in 2..1) { }
}
}"""
fun f() {
for (i in 2..2) {
for (i in 2..1) { }
}
}"""
assertThat(subject.compileAndLint(code)).hasSize(1)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ class LateinitUsageSpec : Spek({

describe("LateinitUsage rule") {
val code = """
import kotlin.SinceKotlin
import kotlin.SinceKotlin
class SomeRandomTest {
lateinit var v1: String
@SinceKotlin("1.0.0") lateinit var v2: String
}
"""
class SomeRandomTest {
lateinit var v1: String
@SinceKotlin("1.0.0") lateinit var v2: String
}
"""

it("should report lateinit usages") {
val findings = LateinitUsage().compileAndLint(code)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@ class UnsafeCastSpec : Spek({

it("does not report cast that might succeed") {
val code = """
fun test(s: Any) {
println(s as Int)
}"""
fun test(s: Any) {
println(s as Int)
}"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
}

it("does not report 'safe' cast that might succeed") {
val code = """
fun test(s: Any) {
println((s as? Int) ?: 0)
}"""
fun test(s: Any) {
println((s as? Int) ?: 0)
}"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,77 +12,77 @@ class UselessPostfixExpressionSpec : Spek({

it("overrides the incremented integer") {
val code = """
fun f() {
var i = 0
i = i-- // invalid
i = 1 + i++ // invalid
i = i++ + 1 // invalid
}"""
fun f() {
var i = 0
i = i-- // invalid
i = 1 + i++ // invalid
i = i++ + 1 // invalid
}"""
assertThat(subject.compileAndLint(code)).hasSize(3)
}

it("does not override the incremented integer") {
val code = """
fun f() {
fun f() {
var i = 0
var j = 0
j = i++
}"""
var j = 0
j = i++
}"""
assertThat(subject.compileAndLint(code)).hasSize(0)
}

it("returns no incremented value") {
val code = """
fun f(): Int {
var i = 0
fun f(): Int {
var i = 0
var j = 0
if (i == 0) return 1 + j++
return i++
}"""
if (i == 0) return 1 + j++
return i++
}"""
assertThat(subject.compileAndLint(code)).hasSize(2)
}

it("should not report field increments") {
val code = """
class Test {
private var runningId: Long = 0
fun increment() {
runningId++
}
fun getId(): Long {
return runningId++
}
}
class Foo(var i: Int = 0) {
fun getIdAndIncrement(): Int {
return i++
}
}
"""
class Test {
private var runningId: Long = 0
fun increment() {
runningId++
}
fun getId(): Long {
return runningId++
}
}
class Foo(var i: Int = 0) {
fun getIdAndIncrement(): Int {
return i++
}
}
"""
assertThat(subject.compileAndLint(code)).isEmpty()
}

it("should detect properties shadowing fields that are incremented") {
val code = """
class Test {
private var runningId: Long = 0
fun getId(): Long {
var runningId: Long = 0
return runningId++
}
}
class Foo(var i: Int = 0) {
fun foo(): Int {
var i = 0
return i++
}
}
"""
class Test {
private var runningId: Long = 0
fun getId(): Long {
var runningId: Long = 0
return runningId++
}
}
class Foo(var i: Int = 0) {
fun foo(): Int {
var i = 0
return i++
}
}
"""
assertThat(subject.compileAndLint(code)).hasSize(2)
}
}
Expand All @@ -93,14 +93,14 @@ class UselessPostfixExpressionSpec : Spek({
val code = """
val str: String? = ""
fun f1(): String {
return str!!
}
fun f1(): String {
return str!!
}
fun f2(): Int {
return str!!.count()
}
"""
fun f2(): Int {
return str!!.count()
}
"""
assertThat(subject.compileAndLint(code)).isEmpty()
}

Expand Down

0 comments on commit 84f8cef

Please sign in to comment.