认识 detekt,一个用于 Kotlin 编程语言的静态代码分析工具。他的功能如下:
- 为您的 Kotlin 项目进行代码气味分析。
- 高度可配置的规则集。
- 生成基线以抑制旧项目的现有问题,同时确保不会引入新问题。
- 使用批注禁止显示源文件中的问题。@Suppress
- 支持不同的报告格式:HTML,Markdown,SARIF,XML(Checkstyle)和自定义报告。
- 使用自定义规则集和报告扩展 detekt。
- 基于代码行数、圈复杂度和代码异味数量的复杂性报告。
- 使用我们的 Gradle 插件与 Gradle 进行第一方集成。
- 一个第三方插件社区,为 detekt 添加了更多规则和功能。
detekt提供了注释,复杂度,协程,空白块,异常,代码格式,库相关,命名,性能,潜在缺陷,代码风格等多个类别的规则,我们整理了其中的246条规则。
使用gui配置工具
使用gui配置工具,可以让配置detekt参数更简单。
在凌鲨(linksaas)的微应用里面可以找到detekt配置助手,快速配置自定义规则。
凌鲨(linksaas)是以开源方式运作的,您可以从linksaas · 极狐GitLab 访问源代码。也可以从 凌鲨 软件研发团队信息枢纽 下载预编译的版本。
附录
规则列表
- AbsentOrWrongFileLicense
- CommentOverPrivateFunction
- CommentOverPrivateProperty
- DeprecatedBlockTag
- EndOfSentenceFormat
- KDocReferencesNonPublicProperty
- OutdatedDocumentation
- UndocumentedPublicClass
- UndocumentedPublicFunction
- UndocumentedPublicProperty
- CognitiveComplexMethod
- ComplexCondition
- ComplexInterface
- CyclomaticComplexMethod
- LabeledExpression
- LargeClass
- LongMethod
- LongParameterList
- MethodOverloading
- NamedArguments
- NestedBlockDepth
- NestedScopeFunctions
- ReplaceSafeCallChainWithRun
- StringLiteralDuplication
- TooManyFunctions
- GlobalCoroutineUsage
- InjectDispatcher
- RedundantSuspendModifier
- SleepInsteadOfDelay
- SuspendFunWithCoroutineScopeReceiver
- SuspendFunWithFlowReturnType
- EmptyCatchBlock
- EmptyClassBlock
- EmptyDefaultConstructor
- EmptyDoWhileBlock
- EmptyElseBlock
- EmptyFinallyBlock
- EmptyForBlock
- EmptyFunctionBlock
- EmptyIfBlock
- EmptyInitBlock
- EmptyKtFile
- EmptySecondaryConstructor
- EmptyTryBlock
- EmptyWhenBlock
- EmptyWhileBlock
- ExceptionRaisedInUnexpectedLocation
- InstanceOfCheckForException
- NotImplementedDeclaration
- ObjectExtendsThrowable
- PrintStackTrace
- RethrowCaughtException
- ReturnFromFinally
- SwallowedException
- ThrowingExceptionFromFinally
- ThrowingExceptionInMain
- ThrowingExceptionsWithoutMessageOrCause
- ThrowingNewInstanceOfSameException
- TooGenericExceptionCaught
- TooGenericExceptionThrown
- AnnotationOnSeparateLine
- AnnotationSpacing
- ArgumentListWrapping
- BlockCommentInitialStarAlignment
- ChainWrapping
- CommentSpacing
- CommentWrapping
- DiscouragedCommentLocation
- EnumEntryNameCase
- Filename
- FinalNewline
- FunKeywordSpacing
- FunctionReturnTypeSpacing
- FunctionSignature
- FunctionStartOfBodySpacing
- FunctionTypeReferenceSpacing
- ImportOrdering
- Indentation
- KdocWrapping
- MaximumLineLength
- ModifierListSpacing
- ModifierOrdering
- MultiLineIfElse
- NoBlankLineBeforeRbrace
- NoBlankLinesInChainedMethodCalls
- NoConsecutiveBlankLines
- NoEmptyClassBody
- NoEmptyFirstLineInMethodBlock
- NoLineBreakAfterElse
- NoLineBreakBeforeAssignment
- NoMultipleSpaces
- NoSemicolons
- NoTrailingSpaces
- NoUnitReturn
- NoUnusedImports
- NoWildcardImports
- NullableTypeSpacing
- PackageName
- ParameterListSpacing
- ParameterListWrapping
- SpacingAroundAngleBrackets
- SpacingAroundColon
- SpacingAroundComma
- SpacingAroundCurly
- SpacingAroundDot
- SpacingAroundDoubleColon
- SpacingAroundKeyword
- SpacingAroundOperators
- SpacingAroundParens
- SpacingAroundRangeOperator
- SpacingAroundUnaryOperator
- SpacingBetweenDeclarationsWithAnnotations
- SpacingBetweenDeclarationsWithComments
- SpacingBetweenFunctionNameAndOpeningParenthesis
- StringTemplate
- TrailingCommaOnCallSite
- TrailingCommaOnDeclarationSite
- TypeArgumentListSpacing
- TypeParameterListSpacing
- UnnecessaryParenthesesBeforeTrailingLambda
- Wrapping
- ForbiddenPublicDataClass
- LibraryCodeMustSpecifyReturnType
- LibraryEntitiesShouldNotBePublic
- BooleanPropertyNaming
- ClassNaming
- ConstructorParameterNaming
- EnumNaming
- ForbiddenClassName
- FunctionMaxLength
- FunctionMinLength
- FunctionNaming
- FunctionParameterNaming
- InvalidPackageDeclaration
- LambdaParameterNaming
- MatchingDeclarationName
- MemberNameEqualsClassName
- NoNameShadowing
- NonBooleanPropertyPrefixedWithIs
- ObjectPropertyNaming
- PackageNaming
- TopLevelPropertyNaming
- VariableMaxLength
- VariableMinLength
- VariableNaming
- ArrayPrimitive
- CouldBeSequence
- ForEachOnRange
- SpreadOperator
- UnnecessaryPartOfBinaryExpression
- UnnecessaryTemporaryInstantiation
- AvoidReferentialEquality
- CastToNullableType
- Deprecation
- DontDowncastCollectionTypes
- DoubleMutabilityForCollection
- ElseCaseInsteadOfExhaustiveWhen
- EqualsAlwaysReturnsTrueOrFalse
- EqualsWithHashCodeExist
- ExitOutsideMain
- ExplicitGarbageCollectionCall
- HasPlatformType
- IgnoredReturnValue
- ImplicitDefaultLocale
- ImplicitUnitReturnType
- InvalidRange
- IteratorHasNextCallsNextMethod
- IteratorNotThrowingNoSuchElementException
- LateinitUsage
- MapGetWithNotNullAssertionOperator
- MissingPackageDeclaration
- NullCheckOnMutableProperty
- NullableToStringCall
- UnconditionalJumpStatementInLoop
- UnnecessaryNotNullCheck
- UnnecessaryNotNullOperator
- UnnecessarySafeCall
- UnreachableCatchBlock
- UnreachableCode
- UnsafeCallOnNullableType
- UnsafeCast
- UnusedUnaryOperator
- UselessPostfixExpression
- WrongEqualsTypeParameter
- AlsoCouldBeApply
- CanBeNonNullable
- CascadingCallWrapping
- ClassOrdering
- CollapsibleIfStatements
- DataClassContainsFunctions
- DataClassShouldBeImmutable
- DestructuringDeclarationWithTooManyEntries
- EqualsNullCall
- EqualsOnSignatureLine
- ExplicitCollectionElementAccessMethod
- ExplicitItLambdaParameter
- ExpressionBodySyntax
- ForbiddenComment
- ForbiddenImport
- ForbiddenMethodCall
- ForbiddenSuppress
- ForbiddenVoid
- FunctionOnlyReturningConstant
- LoopWithTooManyJumpStatements
- MagicNumber
- MandatoryBracesIfStatements
- MandatoryBracesLoops
- MaxChainedCallsOnSameLine
- MaxLineLength
- MayBeConst
- ModifierOrder
- MultilineLambdaItParameter
- MultilineRawStringIndentation
- NestedClassesVisibility
- NewLineAtEndOfFile
- NoTabs
- NullableBooleanCheck
- ObjectLiteralToLambda
- OptionalAbstractKeyword
- OptionalUnit
- OptionalWhenBraces
- PreferToOverPairSyntax
- ProtectedMemberInFinalClass
- RedundantExplicitType
- RedundantHigherOrderMapUsage
- RedundantVisibilityModifierRule
- ReturnCount
- SafeCast
- SerialVersionUIDInSerializableClass
- SpacingBetweenPackageAndImports
- ThrowsCount
- TrailingWhitespace
- TrimMultilineRawString
- UnderscoresInNumericLiterals
- UnnecessaryAbstractClass
- UnnecessaryAnnotationUseSiteTarget
- UnnecessaryApply
- UnnecessaryBackticks
- UnnecessaryFilter
- UnnecessaryInheritance
- UnnecessaryInnerClass
- UnnecessaryLet
- UnnecessaryParentheses
- UntilInsteadOfRangeTo
- UnusedImports
- UnusedPrivateClass
- UnusedPrivateMember
- UseAnyOrNoneInsteadOfFind
- UseArrayLiteralsInAnnotations
- UseCheckNotNull
- UseCheckOrError
- UseDataClass
- UseEmptyCounterpart
- UseIfEmptyOrIfBlank
- UseIfInsteadOfWhen
- UseIsNullOrEmpty
- UseOrEmpty
- UseRequire
- UseRequireNotNull
- UseSumOfInsteadOfFlatMapSize
- UselessCallOnNotNull
- UtilityClassWithPublicConstructor
- VarCouldBeVal
- WildcardImport