forked from rubocop/rubocop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unused_method_argument.rb
121 lines (108 loc) · 3.42 KB
/
unused_method_argument.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# frozen_string_literal: true
module RuboCop
module Cop
module Lint
# Checks for unused method arguments.
#
# @safety
# Autocorrection for this cop is not safe. There may be metaprogramming attached to the
# method (such as a Sorbet `sig`), YARD annoations, and type signtures in RBI or RBS files,
# which will retain the previous argument names under autocorrection.
#
# @example
# # bad
# def some_method(used, unused, _unused_but_allowed)
# puts used
# end
#
# # good
# def some_method(used, _unused, _unused_but_allowed)
# puts used
# end
#
# @example AllowUnusedKeywordArguments: false (default)
# # bad
# def do_something(used, unused: 42)
# used
# end
#
# @example AllowUnusedKeywordArguments: true
# # good
# def do_something(used, unused: 42)
# used
# end
#
# @example IgnoreEmptyMethods: true (default)
# # good
# def do_something(unused)
# end
#
# @example IgnoreEmptyMethods: false
# # bad
# def do_something(unused)
# end
#
# @example IgnoreNotImplementedMethods: true (default)
# # good
# def do_something(unused)
# raise NotImplementedError
# end
#
# def do_something_else(unused)
# fail "TODO"
# end
#
# @example IgnoreNotImplementedMethods: false
# # bad
# def do_something(unused)
# raise NotImplementedError
# end
#
# def do_something_else(unused)
# fail "TODO"
# end
#
class UnusedMethodArgument < Base
include UnusedArgument
extend AutoCorrector
# @!method not_implemented?(node)
def_node_matcher :not_implemented?, <<~PATTERN
{(send nil? :raise (const {nil? cbase} :NotImplementedError) ...)
(send nil? :fail ...)}
PATTERN
def self.joining_forces
VariableForce
end
private
def autocorrect(corrector, node)
UnusedArgCorrector.correct(corrector, processed_source, node)
end
def check_argument(variable)
return unless variable.method_argument?
return if variable.keyword_argument? && cop_config['AllowUnusedKeywordArguments']
return if ignored_method?(variable.scope.node.body)
super
end
def ignored_method?(body)
(cop_config['IgnoreEmptyMethods'] && body.nil?) ||
(cop_config['IgnoreNotImplementedMethods'] && not_implemented?(body))
end
def message(variable)
message = +"Unused method argument - `#{variable.name}`."
unless variable.keyword_argument?
message << " If it's necessary, use `_` or `_#{variable.name}` " \
"as an argument name to indicate that it won't be used."
end
scope = variable.scope
all_arguments = scope.variables.each_value.select(&:method_argument?)
if all_arguments.none?(&:referenced?)
message << " You can also write as `#{scope.name}(*)` " \
'if you want the method to accept any arguments ' \
"but don't care about them."
end
message
end
end
end
end
end