Skip to content

Unused imports crashes on import statements with trailing comment #2990

Description

@kastiglione

New Issue Checklist

Describe the bug

The unused_import rule does not support import statements with a trailing comment.

return (module, contents.range(of: "import \(module)\n"))

Currently, this causes a crash.

Complete output when running SwiftLint, including the stack trace and command used
2019-12-16 18:08:48.968 swiftlint[61765:2014180] *** Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFString replaceCharactersInRange:withString:]: Range or index out of bounds'
  | *** First throw call stack:
  | (
  | 0   CoreFoundation                      0x00007fff52efccf9 __exceptionPreprocess + 256
  | 1   libobjc.A.dylib                     0x00007fff7daafa17 objc_exception_throw + 48
  | 2   CoreFoundation                      0x00007fff52efcb2b +[NSException raise:format:] + 201
  | 3   CoreFoundation                      0x00007fff52f3ab2a mutateError + 261
  | 4   Foundation                          0x00007fff550bdeba -[NSString stringByReplacingCharactersInRange:withString:] + 142
  | 5   swiftlint                           0x000000010ee9ed8c $s18SwiftLintFramework16UnusedImportRuleV7correct4file17compilerArgumentsSayAA10CorrectionVGAA0aB4FileC_SaySSGtF + 684
  | 6   swiftlint                           0x000000010ed45f03 $s18SwiftLintFramework15CollectedLinterV7correct5usingSayAA10CorrectionVGAA11RuleStorageC_tF + 1219
  | 7   swiftlint                           0x000000010f02f692 $s9swiftlint14AnalyzeCommandV11autocorrect33_D106717C80DA5908B379D5A43478964DLLys6ResultOyyt10Commandant0L5ErrorOyytGGAA06LintOrB7OptionsVFy05SwiftN9Framework15CollectedLinterVcfU_ + 34
  | 8   swiftlint                           0x000000010f0472a8 $s18SwiftLintFramework13ConfigurationV9swiftlintE5visit33_30E847BB8C9F32DCEAC767137A79F9B7LL7linters7visitor7storage18duplicateFileNamesSayAA0abS0CGSayAA15CollectedLinterVG_AD20LintableFilesVisitorVAA11RuleStorageCShySSGtFAlOcfU_yyXEfU0_TA + 40
  | 9   swiftlint                           0x000000010f0472d1 $ss5Error_pIgzo_ytsAA_pIegrzo_TRTA + 17
  | 10  libswiftObjectiveC.dylib            0x00007fff7eff3f4f $s10ObjectiveC15autoreleasepool8invokingxxyKXE_tKlF + 47
  | 11  swiftlint                           0x000000010f0462a1 $s18SwiftLintFramework13ConfigurationV9swiftlintE5visit33_30E847BB8C9F32DCEAC767137A79F9B7LL7linters7visitor7storage18duplicateFileNamesSayAA0abS0CGSayAA15CollectedLinterVG_AD20LintableFilesVisitorVAA11RuleStorageCShySSGtFAlOcfU_ + 721
  | 12  swiftlint                           0x000000010f047269 $s18SwiftLintFramework15CollectedLinterVAA0aB4FileCIggo_AcEIegnr_TRTA + 57
  | 13  swiftlint                           0x000000010f0409e4 $sSa9swiftlintE11parallelMap9transformSayqd__Gqd__xXE_tlFADSryqd__SgGzXEfU_ySiXEfU_18SwiftLintFramework15CollectedLinterV_AG0eF4FileCTg5 + 740
  | 14  libswiftDispatch.dylib              0x00007fff7edefaf1 $sSiIgy_SiIegy_TRTA + 17
  | 15  libswiftDispatch.dylib              0x00007fff7edeab34 $sSiIegy_SiIyBy_TR + 20
  | 16  libdispatch.dylib                   0x00007fff7f290672 _dispatch_client_callout2 + 8
  | 17  libdispatch.dylib                   0x00007fff7f29ff95 _dispatch_apply_invoke + 157
  | 18  libdispatch.dylib                   0x00007fff7f29063d _dispatch_client_callout + 8
  | 19  libdispatch.dylib                   0x00007fff7f29e509 _dispatch_root_queue_drain + 657
  | 20  libdispatch.dylib                   0x00007fff7f29eb46 _dispatch_worker_thread2 + 90
  | 21  libsystem_pthread.dylib             0x00007fff7f4ca6b3 _pthread_wqthread + 583
  | 22  libsystem_pthread.dylib             0x00007fff7f4ca3fd start_wqthread + 13
  | )
  | libc++abi.dylib: terminating with uncaught exception of type NSException

Example

// This triggers a violation:
import Foo // Some comment about this

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions