Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable -Werror + fix all warnings #1898

Merged
merged 2 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
@file:JvmName("FacadeFile")
@file:FileAnnotation("file annotations!")
@file:Suppress("unused", "UNUSED_PARAMETER")

package com.squareup.kotlinpoet.metadata.specs

Expand Down
8 changes: 8 additions & 0 deletions kotlinpoet/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi

plugins {
kotlin("multiplatform")
}
Expand All @@ -35,6 +37,12 @@ kotlin {
withJava()
}

@OptIn(ExperimentalKotlinGradlePluginApi::class)
compilerOptions {
allWarningsAsErrors.set(true)
optIn.add("com.squareup.kotlinpoet.DelicateKotlinPoetApi")
}

sourceSets {
val commonMain by getting {
dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ internal fun buildCodeString(
* Converts a [FileSpec] to a string suitable to both human- and kotlinc-consumption. This honors
* imports, indentation, and deferred variable names.
*/
internal class CodeWriter constructor(
internal class CodeWriter(
out: Appendable,
private val indent: String = DEFAULT_INDENT,
imports: Map<String, Import> = emptyMap(),
Expand Down Expand Up @@ -687,6 +687,7 @@ internal class CodeWriter constructor(
LineWrapper(out, indent = DEFAULT_INDENT, columnLimit = Int.MAX_VALUE).use { newOut ->
val oldOut = codeWrapper.out
codeWrapper.out = newOut
@Suppress("UNUSED_EXPRESSION", "unused")
action()
codeWrapper.out = oldOut
}
Expand Down Expand Up @@ -719,14 +720,14 @@ internal class CodeWriter constructor(
val suggestedTypeImports = importsCollector.suggestedTypeImports()
.generateImports(
generatedImports,
canonicalName = ClassName::canonicalName,
computeCanonicalName = ClassName::canonicalName,
capitalizeAliases = true,
referencedNames = importsCollector.referencedNames,
)
val suggestedMemberImports = importsCollector.suggestedMemberImports()
.generateImports(
generatedImports,
canonicalName = MemberName::canonicalName,
computeCanonicalName = MemberName::canonicalName,
capitalizeAliases = false,
referencedNames = importsCollector.referencedNames,
)
Expand All @@ -743,20 +744,20 @@ internal class CodeWriter constructor(

private fun <T> Map<String, Set<T>>.generateImports(
generatedImports: MutableMap<String, Import>,
canonicalName: T.() -> String,
computeCanonicalName: T.() -> String,
capitalizeAliases: Boolean,
referencedNames: Set<String>,
): Map<String, T> {
return flatMap { (simpleName, qualifiedNames) ->
if (qualifiedNames.size == 1 && simpleName !in referencedNames) {
listOf(simpleName to qualifiedNames.first()).also {
val canonicalName = qualifiedNames.first().canonicalName()
val canonicalName = qualifiedNames.first().computeCanonicalName()
generatedImports[canonicalName] = Import(canonicalName)
}
} else {
generateImportAliases(simpleName, qualifiedNames, canonicalName, capitalizeAliases)
generateImportAliases(simpleName, qualifiedNames, computeCanonicalName, capitalizeAliases)
.onEach { (alias, qualifiedName) ->
val canonicalName = qualifiedName.canonicalName()
val canonicalName = qualifiedName.computeCanonicalName()
generatedImports[canonicalName] = Import(canonicalName, alias)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public class FileSpec private constructor(
} catch (e: Exception) {
try {
filerSourceFile.delete()
} catch (ignored: Exception) {
} catch (_: Exception) {
}
throw e
}
Expand Down Expand Up @@ -345,7 +345,7 @@ public class FileSpec private constructor(
}

public fun addImport(constant: Enum<*>): Builder = addImport(
(constant as java.lang.Enum<*>).declaringClass.asClassName(),
constant.declaringJavaClass.asClassName(),
constant.name,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public interface MemberSpecHolder {
public val funSpecs: List<FunSpec>

public interface Builder<out T : Builder<T>> {
@Suppress("UNCHECKED_CAST")
public fun addProperties(propertySpecs: Iterable<PropertySpec>): T = apply {
propertySpecs.map(::addProperty)
} as T
Expand Down Expand Up @@ -56,6 +57,7 @@ public interface MemberSpecHolder {
public fun addProperty(name: String, type: KClass<*>, modifiers: Iterable<KModifier>): T =
addProperty(name, type.asTypeName(), modifiers)

@Suppress("UNCHECKED_CAST")
public fun addFunctions(funSpecs: Iterable<FunSpec>): T = apply {
funSpecs.forEach(::addFunction)
} as T
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public class ParameterSpec private constructor(
ReplaceWith(""),
level = ERROR,
)
public fun jvmModifiers(modifiers: Iterable<Modifier>): Builder = apply {
public fun jvmModifiers(@Suppress("UNUSED_PARAMETER", "unused") modifiers: Iterable<Modifier>): Builder = apply {
throw IllegalArgumentException("JVM modifiers are not permitted on parameters in Kotlin")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -553,12 +553,12 @@ class CodeBlockTest {
)
}

@Test fun `%N escapes keywords`() {
@Test fun `N escapes keywords`() {
val funSpec = FunSpec.builder("object").build()
assertThat(CodeBlock.of("%N", funSpec).toString()).isEqualTo("`object`")
}

@Test fun `%N escapes spaces`() {
@Test fun `N escapes spaces`() {
val funSpec = FunSpec.builder("create taco").build()
assertThat(CodeBlock.of("%N", funSpec).toString()).isEqualTo("`create taco`")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1174,6 +1174,7 @@ class FileSpecTest {
class OhNoThisDoesNotCompile

@Test fun longCommentWithTypes() {
@Suppress("REDUNDANT_PROJECTION")
val someLongParameterizedTypeName = typeNameOf<List<Map<in String, Collection<Map<WackyKey, out OhNoThisDoesNotCompile>>>>>()
val param = ParameterSpec.builder("foo", someLongParameterizedTypeName).build()
val someLongLambdaTypeName = LambdaTypeName.get(STRING, listOf(param), STRING).copy(suspending = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ class FunSpecTest {
}

companion object {
@JvmStatic open fun staticMethod() {
@Suppress("NON_FINAL_MEMBER_IN_OBJECT")
@JvmStatic
open fun staticMethod() {
}
}
}
Expand Down Expand Up @@ -121,6 +123,8 @@ class FunSpecTest {
val classType = classElement.asType() as DeclaredType
val methods = methodsIn(elements.getAllMembers(classElement))
var exec = findFirst(methods, "call")

@Suppress("DEPRECATION")
var funSpec = FunSpec.overriding(exec, classType, types).build()
assertThat(funSpec.toString()).isEqualTo(
"""
Expand All @@ -131,6 +135,7 @@ class FunSpecTest {
""".trimMargin(),
)
exec = findFirst(methods, "compareTo")
@Suppress("DEPRECATION")
funSpec = FunSpec.overriding(exec, classType, types).build()
assertThat(funSpec.toString()).isEqualTo(
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ class MemberNameTest {
.isEqualTo(MemberName(ClassName("kotlin.text", "Regex"), "fromLiteral"))
}

@Test fun `%N escapes MemberNames`() {
@Test fun `N escapes MemberNames`() {
val taco = ClassName("com.squareup.tacos", "Taco")
val packager = ClassName("com.squareup.tacos", "TacoPackager")
val file = FileSpec.builder("com.example", "Test")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ class TypeVariableNameTest {
}

@Test fun emptyBoundsShouldDefaultToAnyNullable() {
val typeVariable = TypeVariableName("E", bounds = *emptyArray<TypeName>())
val typeVariable = TypeVariableName("E", bounds = emptyArray<TypeName>())
val typeSpec = TypeSpec.classBuilder("Taco")
.addTypeVariable(typeVariable)
.build()
Expand Down