SwiftLint icon indicating copy to clipboard operation
SwiftLint copied to clipboard

`unused-parameter` rule should not trigger on parameters used for their type

Open sindresorhus opened this issue 1 year ago • 2 comments
trafficstars

New Issue Checklist

Bug Description

It should not report for cases where the parameter is used for its type and not value.

extension Data {
	func jsonDecoded<T: Decodable>(ofType type: T.Type) throws -> T {
		try JSONDecoder().decode(T.self, from: self)
	}
}

Here the type parameter decides the return type.

Environment

  • SwiftLint version: 0.56.0
  • Xcode version: Xcode 15.4, Build version 15F31d
  • Installation method used: Installer
  • Configuration file:
only_rules:
  - unused_parameter

sindresorhus avatar Aug 08 '24 20:08 sindresorhus

The rule suggests to write it like:

extension Data {
  func jsonDecoded<T: Decodable>(ofType _: T.Type) throws -> T {
    try JSONDecoder().decode(T.self, from: self)
  }
}

The intention is not to remove the parameter entirely if that's infeasible.

SimplyDanny avatar Aug 09 '24 08:08 SimplyDanny

My argument is that the parameter is technically used when used for its type, not its value. My thinking is that, the parameter is used if removing the parameter makes the function no longer compile.

sindresorhus avatar Aug 18 '24 08:08 sindresorhus