reek-rs 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/Cargo.lock +614 -0
- data/Cargo.toml +24 -0
- data/DIVERGENCES.md +15 -0
- data/LICENSE +22 -0
- data/README.md +125 -0
- data/THIRD_PARTY_NOTICES.md +57 -0
- data/crates/reek-cli/Cargo.toml +22 -0
- data/crates/reek-cli/src/main.rs +277 -0
- data/crates/reek-core/Cargo.toml +21 -0
- data/crates/reek-core/src/ast.rs +449 -0
- data/crates/reek-core/src/code_comment.rs +245 -0
- data/crates/reek-core/src/comments.rs +179 -0
- data/crates/reek-core/src/config.rs +803 -0
- data/crates/reek-core/src/context.rs +891 -0
- data/crates/reek-core/src/detectors/attribute.rs +13 -0
- data/crates/reek-core/src/detectors/boolean_parameter.rs +20 -0
- data/crates/reek-core/src/detectors/class_variable.rs +25 -0
- data/crates/reek-core/src/detectors/control_parameter.rs +110 -0
- data/crates/reek-core/src/detectors/data_clump.rs +114 -0
- data/crates/reek-core/src/detectors/duplicate_method_call.rs +63 -0
- data/crates/reek-core/src/detectors/feature_envy.rs +32 -0
- data/crates/reek-core/src/detectors/instance_variable_assumption.rs +39 -0
- data/crates/reek-core/src/detectors/irresponsible_module.rs +12 -0
- data/crates/reek-core/src/detectors/long_parameter_list.rs +16 -0
- data/crates/reek-core/src/detectors/long_yield_list.rs +20 -0
- data/crates/reek-core/src/detectors/manual_dispatch.rs +18 -0
- data/crates/reek-core/src/detectors/missing_safe_method.rs +34 -0
- data/crates/reek-core/src/detectors/mod.rs +479 -0
- data/crates/reek-core/src/detectors/module_initialize.rs +15 -0
- data/crates/reek-core/src/detectors/nested_iterators.rs +82 -0
- data/crates/reek-core/src/detectors/nil_check.rs +40 -0
- data/crates/reek-core/src/detectors/repeated_conditional.rs +51 -0
- data/crates/reek-core/src/detectors/subclassed_from_core_class.rs +22 -0
- data/crates/reek-core/src/detectors/too_many_constants.rs +18 -0
- data/crates/reek-core/src/detectors/too_many_instance_variables.rs +25 -0
- data/crates/reek-core/src/detectors/too_many_methods.rs +15 -0
- data/crates/reek-core/src/detectors/too_many_statements.rs +16 -0
- data/crates/reek-core/src/detectors/uncommunicative_method_name.rs +32 -0
- data/crates/reek-core/src/detectors/uncommunicative_module_name.rs +33 -0
- data/crates/reek-core/src/detectors/uncommunicative_parameter_name.rs +49 -0
- data/crates/reek-core/src/detectors/uncommunicative_variable_name.rs +138 -0
- data/crates/reek-core/src/detectors/unused_parameters.rs +21 -0
- data/crates/reek-core/src/detectors/unused_private_method.rs +59 -0
- data/crates/reek-core/src/detectors/utility_function.rs +27 -0
- data/crates/reek-core/src/encodings.rs +180 -0
- data/crates/reek-core/src/examiner.rs +165 -0
- data/crates/reek-core/src/lib.rs +21 -0
- data/crates/reek-core/src/node.rs +423 -0
- data/crates/reek-core/src/ruby_format.rs +60 -0
- data/crates/reek-core/src/ruby_inspect.rs +172 -0
- data/crates/reek-core/src/ruby_regexp.rs +243 -0
- data/crates/reek-core/src/single_byte_encodings.rs +6007 -0
- data/crates/reek-core/src/source.rs +280 -0
- data/crates/reek-core/src/translate.rs +3112 -0
- data/crates/reek-core/src/yaml.rs +520 -0
- data/ext/reek_rs/Cargo.toml +18 -0
- data/ext/reek_rs/extconf.rb +6 -0
- data/ext/reek_rs/src/lib.rs +155 -0
- data/lib/reek_rs/version.rb +8 -0
- data/lib/reek_rs.rb +390 -0
- metadata +123 -0
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
//! `SubclassedFromCoreClass`: a class inheriting from `Array`, `Hash` or
|
|
2
|
+
//! `String`.
|
|
3
|
+
|
|
4
|
+
use super::{Detector, Parameter, SniffResult};
|
|
5
|
+
use crate::node::Name;
|
|
6
|
+
|
|
7
|
+
/// `CORE_CLASSES`.
|
|
8
|
+
const CORE_CLASSES: [&str; 3] = ["Array", "Hash", "String"];
|
|
9
|
+
|
|
10
|
+
pub fn sniff(detector: &Detector) -> SniffResult {
|
|
11
|
+
let tree = detector.tree();
|
|
12
|
+
let Some(superclass) = tree.superclass(detector.expression()) else { return Ok(Vec::new()) };
|
|
13
|
+
let Name::String(superclass_name) = tree.name(superclass) else { return Ok(Vec::new()) };
|
|
14
|
+
if !CORE_CLASSES.contains(&superclass_name.as_str()) {
|
|
15
|
+
return Ok(Vec::new());
|
|
16
|
+
}
|
|
17
|
+
Ok(vec![detector.smell_warning(
|
|
18
|
+
vec![detector.source_line()?],
|
|
19
|
+
format!("inherits from core class '{superclass_name}'"),
|
|
20
|
+
vec![("ancestor", Parameter::String(superclass_name))],
|
|
21
|
+
)?])
|
|
22
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
//! `TooManyConstants`: a class or module assigning more than
|
|
2
|
+
//! `max_constants` constants (not counting module definitions).
|
|
3
|
+
|
|
4
|
+
use super::{exceeds, Detector, Parameter, SniffResult};
|
|
5
|
+
|
|
6
|
+
pub fn sniff(detector: &Detector) -> SniffResult {
|
|
7
|
+
let tree = detector.tree();
|
|
8
|
+
let casgns = detector.contexts.local_nodes(detector.context, &["casgn"], &[]);
|
|
9
|
+
let count = casgns.into_iter().filter(|casgn| !tree.defines_module(*casgn)).count() as i64;
|
|
10
|
+
if !exceeds(count, &detector.value("max_constants")?)? {
|
|
11
|
+
return Ok(Vec::new());
|
|
12
|
+
}
|
|
13
|
+
Ok(vec![detector.smell_warning(
|
|
14
|
+
vec![detector.source_line()?],
|
|
15
|
+
format!("has {count} constants"),
|
|
16
|
+
vec![("count", Parameter::Integer(count))],
|
|
17
|
+
)?])
|
|
18
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
//! `TooManyInstanceVariables`: a class assigning more than
|
|
2
|
+
//! `max_instance_variables` instance variables (outside `||=`).
|
|
3
|
+
|
|
4
|
+
use super::{exceeds, Detector, Parameter, SniffResult};
|
|
5
|
+
use crate::node::Name;
|
|
6
|
+
|
|
7
|
+
pub fn sniff(detector: &Detector) -> SniffResult {
|
|
8
|
+
let tree = detector.tree();
|
|
9
|
+
let mut variables: Vec<Name> = Vec::new();
|
|
10
|
+
for ivasgn in detector.contexts.local_nodes(detector.context, &["ivasgn"], &["or_asgn"]) {
|
|
11
|
+
let name = tree.name(ivasgn);
|
|
12
|
+
if !variables.contains(&name) {
|
|
13
|
+
variables.push(name);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
let count = variables.len() as i64;
|
|
17
|
+
if !exceeds(count, &detector.value("max_instance_variables")?)? {
|
|
18
|
+
return Ok(Vec::new());
|
|
19
|
+
}
|
|
20
|
+
Ok(vec![detector.smell_warning(
|
|
21
|
+
vec![detector.source_line()?],
|
|
22
|
+
format!("has at least {count} instance variables"),
|
|
23
|
+
vec![("count", Parameter::Integer(count))],
|
|
24
|
+
)?])
|
|
25
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
//! `TooManyMethods`: a class defining more than `max_methods` methods.
|
|
2
|
+
|
|
3
|
+
use super::{exceeds, Detector, Parameter, SniffResult};
|
|
4
|
+
|
|
5
|
+
pub fn sniff(detector: &Detector) -> SniffResult {
|
|
6
|
+
let actual = detector.contexts.node_instance_methods(detector.context).len() as i64;
|
|
7
|
+
if !exceeds(actual, &detector.value("max_methods")?)? {
|
|
8
|
+
return Ok(Vec::new());
|
|
9
|
+
}
|
|
10
|
+
Ok(vec![detector.smell_warning(
|
|
11
|
+
vec![detector.source_line()?],
|
|
12
|
+
format!("has at least {actual} methods"),
|
|
13
|
+
vec![("count", Parameter::Integer(actual))],
|
|
14
|
+
)?])
|
|
15
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
//! `TooManyStatements`: a method with more than `max_statements`
|
|
2
|
+
//! statements, as `ContextBuilder` counts them.
|
|
3
|
+
|
|
4
|
+
use super::{exceeds, Detector, Parameter, SniffResult};
|
|
5
|
+
|
|
6
|
+
pub fn sniff(detector: &Detector) -> SniffResult {
|
|
7
|
+
let count = detector.contexts.number_of_statements(detector.context);
|
|
8
|
+
if !exceeds(count, &detector.value("max_statements")?)? {
|
|
9
|
+
return Ok(Vec::new());
|
|
10
|
+
}
|
|
11
|
+
Ok(vec![detector.smell_warning(
|
|
12
|
+
vec![detector.source_line()?],
|
|
13
|
+
format!("has approx {count} statements"),
|
|
14
|
+
vec![("count", Parameter::Integer(count))],
|
|
15
|
+
)?])
|
|
16
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
//! `UncommunicativeMethodName`: a method name matching a `reject` pattern
|
|
2
|
+
//! and no `accept` pattern.
|
|
3
|
+
|
|
4
|
+
use super::{ruby_array, string_match, Detector, Parameter, SniffResult};
|
|
5
|
+
use crate::context::ContextError;
|
|
6
|
+
|
|
7
|
+
pub fn sniff(detector: &Detector) -> SniffResult {
|
|
8
|
+
let name = detector.contexts.name(detector.context).to_s().to_string();
|
|
9
|
+
if is_acceptable_name(detector, &name)? {
|
|
10
|
+
return Ok(Vec::new());
|
|
11
|
+
}
|
|
12
|
+
Ok(vec![detector.smell_warning(
|
|
13
|
+
vec![detector.source_line()?],
|
|
14
|
+
format!("has the name '{name}'"),
|
|
15
|
+
vec![("name", Parameter::String(name))],
|
|
16
|
+
)?])
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/// `acceptable_name?`.
|
|
20
|
+
fn is_acceptable_name(detector: &Detector, name: &str) -> Result<bool, ContextError> {
|
|
21
|
+
for accept_pattern in ruby_array(detector.value("accept")?) {
|
|
22
|
+
if string_match(name, &accept_pattern)? {
|
|
23
|
+
return Ok(true);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
for reject_pattern in ruby_array(detector.value("reject")?) {
|
|
27
|
+
if string_match(name, &reject_pattern)? {
|
|
28
|
+
return Ok(false);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
Ok(true)
|
|
32
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
//! `UncommunicativeModuleName`: a module or class whose name matches a
|
|
2
|
+
//! `reject` pattern, unless its full name matches an `accept` pattern.
|
|
3
|
+
|
|
4
|
+
use super::{ruby_array, string_match, Detector, Parameter, SniffResult};
|
|
5
|
+
use crate::context::ContextError;
|
|
6
|
+
|
|
7
|
+
pub fn sniff(detector: &Detector) -> SniffResult {
|
|
8
|
+
let fully_qualified_name = detector.contexts.full_name(detector.context)?;
|
|
9
|
+
let module_name = detector.tree().module_simple_name(detector.expression());
|
|
10
|
+
if is_acceptable_name(detector, &module_name, &fully_qualified_name)? {
|
|
11
|
+
return Ok(Vec::new());
|
|
12
|
+
}
|
|
13
|
+
Ok(vec![detector.smell_warning(
|
|
14
|
+
vec![detector.source_line()?],
|
|
15
|
+
format!("has the name '{module_name}'"),
|
|
16
|
+
vec![("name", Parameter::String(module_name))],
|
|
17
|
+
)?])
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/// `acceptable_name?(module_name:, fully_qualified_name:)`.
|
|
21
|
+
fn is_acceptable_name(detector: &Detector, module_name: &str, fully_qualified_name: &str) -> Result<bool, ContextError> {
|
|
22
|
+
for accept_pattern in ruby_array(detector.value("accept")?) {
|
|
23
|
+
if string_match(fully_qualified_name, &accept_pattern)? {
|
|
24
|
+
return Ok(true);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
for reject_pattern in ruby_array(detector.value("reject")?) {
|
|
28
|
+
if string_match(module_name, &reject_pattern)? {
|
|
29
|
+
return Ok(false);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
Ok(true)
|
|
33
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
//! `UncommunicativeParameterName`: a parameter name matching a `reject`
|
|
2
|
+
//! pattern and no `accept` pattern, unless it is marked unused (`_name`)
|
|
3
|
+
//! and not used.
|
|
4
|
+
|
|
5
|
+
use super::{ruby_array, string_match, Detector, Parameter, SniffResult};
|
|
6
|
+
use crate::ast::NodeId;
|
|
7
|
+
use crate::context::ContextError;
|
|
8
|
+
|
|
9
|
+
pub fn sniff(detector: &Detector) -> SniffResult {
|
|
10
|
+
let tree = detector.tree();
|
|
11
|
+
let mut smells = Vec::new();
|
|
12
|
+
for parameter in tree.parameters(detector.expression())? {
|
|
13
|
+
if !is_uncommunicative_parameter(detector, parameter)? {
|
|
14
|
+
continue;
|
|
15
|
+
}
|
|
16
|
+
let name = tree.name(parameter).to_s().to_string();
|
|
17
|
+
smells.push(detector.smell_warning(
|
|
18
|
+
vec![detector.source_line()?],
|
|
19
|
+
format!("has the parameter name '{name}'"),
|
|
20
|
+
vec![("name", Parameter::String(name))],
|
|
21
|
+
)?);
|
|
22
|
+
}
|
|
23
|
+
Ok(smells)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/// `uncommunicative_parameter?`.
|
|
27
|
+
fn is_uncommunicative_parameter(detector: &Detector, parameter: NodeId) -> Result<bool, ContextError> {
|
|
28
|
+
let tree = detector.tree();
|
|
29
|
+
let plain_name = tree.name(parameter).to_s().to_string();
|
|
30
|
+
if is_acceptable_name(detector, &plain_name)? {
|
|
31
|
+
return Ok(false);
|
|
32
|
+
}
|
|
33
|
+
Ok(detector.contexts.uses_param(detector.context, parameter) || !tree.is_marked_unused(parameter))
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/// `acceptable_name?`.
|
|
37
|
+
fn is_acceptable_name(detector: &Detector, name: &str) -> Result<bool, ContextError> {
|
|
38
|
+
for accept_pattern in ruby_array(detector.value("accept")?) {
|
|
39
|
+
if string_match(name, &accept_pattern)? {
|
|
40
|
+
return Ok(true);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
for reject_pattern in ruby_array(detector.value("reject")?) {
|
|
44
|
+
if string_match(name, &reject_pattern)? {
|
|
45
|
+
return Ok(false);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
Ok(true)
|
|
49
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
//! `UncommunicativeVariableName`: a local variable, block parameter, or (in
|
|
2
|
+
//! a class or module) instance variable whose name, without leading `@`,
|
|
3
|
+
//! `*` and `&`, matches a `reject` pattern and no `accept` pattern.
|
|
4
|
+
|
|
5
|
+
use std::cell::OnceCell;
|
|
6
|
+
|
|
7
|
+
use indexmap::IndexMap;
|
|
8
|
+
|
|
9
|
+
use super::{line, ruby_array, string_match, Detector, Parameter, SniffResult};
|
|
10
|
+
use crate::ast::{Child, NodeId};
|
|
11
|
+
use crate::context::ContextError;
|
|
12
|
+
use crate::yaml::Value;
|
|
13
|
+
|
|
14
|
+
/// The types a variable search does not descend into.
|
|
15
|
+
const SCOPE_TYPES: [&str; 4] = ["class", "module", "defs", "def"];
|
|
16
|
+
|
|
17
|
+
pub fn sniff(detector: &Detector) -> SniffResult {
|
|
18
|
+
let names = Patterns { detector, accept_names: OnceCell::new(), reject_names: OnceCell::new() };
|
|
19
|
+
let mut smells = Vec::new();
|
|
20
|
+
for (name, lines) in variable_names(detector)? {
|
|
21
|
+
if !names.is_uncommunicative_variable_name(&name)? {
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
24
|
+
smells.push(detector.smell_warning(
|
|
25
|
+
lines,
|
|
26
|
+
format!("has the variable name '{name}'"),
|
|
27
|
+
vec![("name", Parameter::String(name))],
|
|
28
|
+
)?);
|
|
29
|
+
}
|
|
30
|
+
Ok(smells)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/// `variable_names`: each name assigned, then each block parameter, with
|
|
34
|
+
/// the lines it appears on.
|
|
35
|
+
fn variable_names(detector: &Detector) -> Result<IndexMap<String, Vec<usize>>, ContextError> {
|
|
36
|
+
let mut result: IndexMap<String, Vec<usize>> = IndexMap::new();
|
|
37
|
+
find_assignment_variable_names(detector, &mut result)?;
|
|
38
|
+
find_block_argument_variable_names(detector, &mut result)?;
|
|
39
|
+
Ok(result)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/// `find_assignment_variable_names`: local variables assigned in the
|
|
43
|
+
/// context's own scope, and in a class or module, instance variables
|
|
44
|
+
/// assigned anywhere in it outside nested classes and modules.
|
|
45
|
+
fn find_assignment_variable_names(detector: &Detector, accumulator: &mut IndexMap<String, Vec<usize>>) -> Result<(), ContextError> {
|
|
46
|
+
let tree = detector.tree();
|
|
47
|
+
let expression = detector.expression();
|
|
48
|
+
let mut assignment_nodes = tree.each_node(expression, &["lvasgn"], &SCOPE_TYPES);
|
|
49
|
+
if matches!(tree.ty(expression), "class" | "module") {
|
|
50
|
+
assignment_nodes.extend(tree.each_node(expression, &["ivasgn"], &["class", "module"]));
|
|
51
|
+
}
|
|
52
|
+
for asgn in assignment_nodes {
|
|
53
|
+
let name = tree.symbol_child(asgn, 0).expect("an assignment names its variable");
|
|
54
|
+
accumulator.entry(name).or_default().push(line(tree, asgn)?);
|
|
55
|
+
}
|
|
56
|
+
Ok(())
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/// `find_block_argument_variable_names`: the parameters of the blocks in a
|
|
60
|
+
/// class or module, or in a method's body, outside nested scopes.
|
|
61
|
+
fn find_block_argument_variable_names(detector: &Detector, accumulator: &mut IndexMap<String, Vec<usize>>) -> Result<(), ContextError> {
|
|
62
|
+
let tree = detector.tree();
|
|
63
|
+
let expression = detector.expression();
|
|
64
|
+
let arg_search_exp = match tree.ty(expression) {
|
|
65
|
+
"class" | "module" => Some(expression),
|
|
66
|
+
"defs" | "def" => tree.method_body(expression),
|
|
67
|
+
_ => None,
|
|
68
|
+
};
|
|
69
|
+
let Some(arg_search_exp) = arg_search_exp else { return Ok(()) };
|
|
70
|
+
for args_node in tree.each_node(arg_search_exp, &["args"], &SCOPE_TYPES) {
|
|
71
|
+
recursively_record_variable_names(detector, accumulator, args_node)?;
|
|
72
|
+
}
|
|
73
|
+
Ok(())
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/// `recursively_record_variable_names`: each parameter's name, recorded on
|
|
77
|
+
/// the line of the `args` or `mlhs` node it is in.
|
|
78
|
+
fn recursively_record_variable_names(
|
|
79
|
+
detector: &Detector,
|
|
80
|
+
accumulator: &mut IndexMap<String, Vec<usize>>,
|
|
81
|
+
exp: NodeId,
|
|
82
|
+
) -> Result<(), ContextError> {
|
|
83
|
+
let tree = detector.tree();
|
|
84
|
+
for subexp in tree.children(exp) {
|
|
85
|
+
let Child::Node(subexp) = subexp else {
|
|
86
|
+
return Err(ContextError::Unexpected("undefined method 'type' for a non-node child".to_string()));
|
|
87
|
+
};
|
|
88
|
+
if tree.ty(*subexp) == "mlhs" {
|
|
89
|
+
recursively_record_variable_names(detector, accumulator, *subexp)?;
|
|
90
|
+
} else {
|
|
91
|
+
// `record_variable_name`
|
|
92
|
+
let name = tree.name(*subexp);
|
|
93
|
+
let varname = name.to_s().strip_prefix('*').unwrap_or(name.to_s());
|
|
94
|
+
if varname.is_empty() {
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
accumulator.entry(varname.to_string()).or_default().push(line(tree, exp)?);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
Ok(())
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/// The `accept` and `reject` patterns, read when first needed.
|
|
104
|
+
struct Patterns<'a, 'b, 'tree> {
|
|
105
|
+
detector: &'a Detector<'b, 'tree>,
|
|
106
|
+
accept_names: OnceCell<Vec<Value>>,
|
|
107
|
+
reject_names: OnceCell<Vec<Value>>,
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
impl Patterns<'_, '_, '_> {
|
|
111
|
+
/// `uncommunicative_variable_name?`.
|
|
112
|
+
fn is_uncommunicative_variable_name(&self, name: &str) -> Result<bool, ContextError> {
|
|
113
|
+
let sanitized_name = name.trim_start_matches(['@', '*', '&']);
|
|
114
|
+
Ok(!self.is_acceptable_name(sanitized_name)?)
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/// `acceptable_name?`.
|
|
118
|
+
fn is_acceptable_name(&self, name: &str) -> Result<bool, ContextError> {
|
|
119
|
+
for accept_pattern in self.patterns(&self.accept_names, "accept")? {
|
|
120
|
+
if string_match(name, accept_pattern)? {
|
|
121
|
+
return Ok(true);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
for reject_pattern in self.patterns(&self.reject_names, "reject")? {
|
|
125
|
+
if string_match(name, reject_pattern)? {
|
|
126
|
+
return Ok(false);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
Ok(true)
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
fn patterns<'c>(&self, cell: &'c OnceCell<Vec<Value>>, key: &str) -> Result<&'c Vec<Value>, ContextError> {
|
|
133
|
+
if cell.get().is_none() {
|
|
134
|
+
let _ = cell.set(ruby_array(self.detector.value(key)?));
|
|
135
|
+
}
|
|
136
|
+
Ok(cell.get().expect("set above"))
|
|
137
|
+
}
|
|
138
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
//! `UnusedParameters`: a parameter the method never uses, unless the
|
|
2
|
+
//! method calls `super` without arguments (passing them on).
|
|
3
|
+
|
|
4
|
+
use super::{Detector, Parameter, SniffResult};
|
|
5
|
+
|
|
6
|
+
pub fn sniff(detector: &Detector) -> SniffResult {
|
|
7
|
+
let contexts = detector.contexts;
|
|
8
|
+
if contexts.uses_super_with_implicit_arguments(detector.context) {
|
|
9
|
+
return Ok(Vec::new());
|
|
10
|
+
}
|
|
11
|
+
let mut smells = Vec::new();
|
|
12
|
+
for param in contexts.unused_params(detector.context)? {
|
|
13
|
+
let name = detector.tree().name(param).to_s().to_string();
|
|
14
|
+
smells.push(detector.smell_warning(
|
|
15
|
+
vec![detector.source_line()?],
|
|
16
|
+
format!("has unused parameter '{name}'"),
|
|
17
|
+
vec![("name", Parameter::String(name))],
|
|
18
|
+
)?);
|
|
19
|
+
}
|
|
20
|
+
Ok(smells)
|
|
21
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
//! `UnusedPrivateMethod` (disabled by default): a private instance method
|
|
2
|
+
//! the class's instance methods never call (directly or with `method`).
|
|
3
|
+
|
|
4
|
+
use super::{array_items, line, Detector, Parameter, SniffResult};
|
|
5
|
+
use crate::context::{ContextError, ContextId, Visibility};
|
|
6
|
+
use crate::node::Name;
|
|
7
|
+
use crate::yaml::Value;
|
|
8
|
+
|
|
9
|
+
pub fn sniff(detector: &Detector) -> SniffResult {
|
|
10
|
+
let contexts = detector.contexts;
|
|
11
|
+
let mut smells = Vec::new();
|
|
12
|
+
for defined_method in unused_private_methods(detector) {
|
|
13
|
+
if is_ignored_method(detector, defined_method)? {
|
|
14
|
+
continue;
|
|
15
|
+
}
|
|
16
|
+
let name = contexts.name(defined_method).to_s().to_string();
|
|
17
|
+
let line = line(detector.tree(), contexts.context(defined_method).exp)?;
|
|
18
|
+
smells.push(detector.smell_warning(
|
|
19
|
+
vec![line],
|
|
20
|
+
format!("has the unused private instance method '{name}'"),
|
|
21
|
+
vec![("name", Parameter::String(name))],
|
|
22
|
+
)?);
|
|
23
|
+
}
|
|
24
|
+
Ok(smells)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/// `unused_private_methods`.
|
|
28
|
+
fn unused_private_methods(detector: &Detector) -> Vec<ContextId> {
|
|
29
|
+
let contexts = detector.contexts;
|
|
30
|
+
let defined_private_methods = contexts.defined_instance_methods(detector.context, Some(Visibility::Private));
|
|
31
|
+
let mut called_method_names: Vec<Name> =
|
|
32
|
+
contexts.instance_method_calls(detector.context).into_iter().map(|call| contexts.name(call)).collect();
|
|
33
|
+
called_method_names.extend(contexts.instance_method_names_via_to_call(detector.context));
|
|
34
|
+
defined_private_methods
|
|
35
|
+
.into_iter()
|
|
36
|
+
.filter(|defined_method| !called_method_names.contains(&contexts.name(*defined_method)))
|
|
37
|
+
.collect()
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/// `ignore_method?`: `Parent#name` contains an `exclude` string or matches
|
|
41
|
+
/// an `exclude` pattern.
|
|
42
|
+
fn is_ignored_method(detector: &Detector, method: ContextId) -> Result<bool, ContextError> {
|
|
43
|
+
let contexts = detector.contexts;
|
|
44
|
+
for ignore_context in array_items(&detector.value("exclude")?, "any?")? {
|
|
45
|
+
let parent = contexts.context(method).parent.expect("a method has a parent");
|
|
46
|
+
let full_name = format!("{}#{}", contexts.full_name(parent)?, contexts.name(method).to_s());
|
|
47
|
+
let matched = match &ignore_context {
|
|
48
|
+
Value::Str(text) => full_name.contains(text.as_str()),
|
|
49
|
+
Value::Regexp(regexp) => regexp.is_match(full_name.as_bytes()),
|
|
50
|
+
other => {
|
|
51
|
+
return Err(ContextError::Unexpected(format!("no implicit conversion of {} into Integer", other.class_name())));
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
if matched {
|
|
55
|
+
return Ok(true);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
Ok(false)
|
|
59
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
//! `UtilityFunction`: an instance method that calls methods but does not
|
|
2
|
+
//! depend on instance state.
|
|
3
|
+
|
|
4
|
+
use super::{Detector, SniffResult};
|
|
5
|
+
|
|
6
|
+
pub fn sniff(detector: &Detector) -> SniffResult {
|
|
7
|
+
let contexts = detector.contexts;
|
|
8
|
+
if contexts.is_singleton_method(detector.context) || contexts.is_module_function(detector.context) {
|
|
9
|
+
return Ok(Vec::new());
|
|
10
|
+
}
|
|
11
|
+
if contexts.references_self(detector.context) {
|
|
12
|
+
return Ok(Vec::new());
|
|
13
|
+
}
|
|
14
|
+
// `num_helper_methods`
|
|
15
|
+
if contexts.local_nodes(detector.context, &["send"], &[]).is_empty() {
|
|
16
|
+
return Ok(Vec::new());
|
|
17
|
+
}
|
|
18
|
+
// `ignore_method?`
|
|
19
|
+
if contexts.has_non_public_visibility(detector.context) && detector.value("public_methods_only")?.is_truthy() {
|
|
20
|
+
return Ok(Vec::new());
|
|
21
|
+
}
|
|
22
|
+
Ok(vec![detector.smell_warning(
|
|
23
|
+
vec![detector.source_line()?],
|
|
24
|
+
"doesn't depend on instance state (maybe move it to another class?)".to_string(),
|
|
25
|
+
Vec::new(),
|
|
26
|
+
)?])
|
|
27
|
+
}
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
//! Ruby 3.3 encoding names, generated from `Encoding.name_list`.
|
|
2
|
+
//!
|
|
3
|
+
//! `Encoding.find` looks names up case-insensitively; each entry maps a
|
|
4
|
+
//! lowercased name or alias to the encoding's name and whether it is ASCII
|
|
5
|
+
//! compatible.
|
|
6
|
+
|
|
7
|
+
/// (lowercased name or alias, `Encoding#name`, `Encoding#ascii_compatible?`)
|
|
8
|
+
pub const ENCODINGS: &[(&str, &str, bool)] = &[
|
|
9
|
+
("646", "US-ASCII", true),
|
|
10
|
+
("ansi_x3.4-1968", "US-ASCII", true),
|
|
11
|
+
("ascii", "US-ASCII", true),
|
|
12
|
+
("ascii-8bit", "ASCII-8BIT", true),
|
|
13
|
+
("big5", "Big5", true),
|
|
14
|
+
("big5-hkscs", "Big5-HKSCS", true),
|
|
15
|
+
("big5-hkscs:2008", "Big5-HKSCS", true),
|
|
16
|
+
("big5-uao", "Big5-UAO", true),
|
|
17
|
+
("binary", "ASCII-8BIT", true),
|
|
18
|
+
("cesu-8", "CESU-8", true),
|
|
19
|
+
("cp1250", "Windows-1250", true),
|
|
20
|
+
("cp1251", "Windows-1251", true),
|
|
21
|
+
("cp1252", "Windows-1252", true),
|
|
22
|
+
("cp1253", "Windows-1253", true),
|
|
23
|
+
("cp1254", "Windows-1254", true),
|
|
24
|
+
("cp1255", "Windows-1255", true),
|
|
25
|
+
("cp1256", "Windows-1256", true),
|
|
26
|
+
("cp1257", "Windows-1257", true),
|
|
27
|
+
("cp1258", "Windows-1258", true),
|
|
28
|
+
("cp437", "IBM437", true),
|
|
29
|
+
("cp50220", "CP50220", false),
|
|
30
|
+
("cp50221", "CP50221", false),
|
|
31
|
+
("cp51932", "CP51932", true),
|
|
32
|
+
("cp65000", "UTF-7", false),
|
|
33
|
+
("cp65001", "UTF-8", true),
|
|
34
|
+
("cp720", "IBM720", true),
|
|
35
|
+
("cp737", "IBM737", true),
|
|
36
|
+
("cp775", "IBM775", true),
|
|
37
|
+
("cp850", "CP850", true),
|
|
38
|
+
("cp852", "CP852", true),
|
|
39
|
+
("cp855", "CP855", true),
|
|
40
|
+
("cp857", "IBM857", true),
|
|
41
|
+
("cp860", "IBM860", true),
|
|
42
|
+
("cp861", "IBM861", true),
|
|
43
|
+
("cp862", "IBM862", true),
|
|
44
|
+
("cp863", "IBM863", true),
|
|
45
|
+
("cp864", "IBM864", true),
|
|
46
|
+
("cp865", "IBM865", true),
|
|
47
|
+
("cp866", "IBM866", true),
|
|
48
|
+
("cp869", "IBM869", true),
|
|
49
|
+
("cp874", "Windows-874", true),
|
|
50
|
+
("cp878", "KOI8-R", true),
|
|
51
|
+
("cp932", "Windows-31J", true),
|
|
52
|
+
("cp936", "GBK", true),
|
|
53
|
+
("cp949", "CP949", true),
|
|
54
|
+
("cp950", "CP950", true),
|
|
55
|
+
("cp951", "CP951", true),
|
|
56
|
+
("cswindows31j", "Windows-31J", true),
|
|
57
|
+
("ebcdic-cp-us", "IBM037", false),
|
|
58
|
+
("emacs-mule", "Emacs-Mule", true),
|
|
59
|
+
("euc-cn", "GB2312", true),
|
|
60
|
+
("euc-jis-2004", "EUC-JIS-2004", true),
|
|
61
|
+
("euc-jisx0213", "EUC-JIS-2004", true),
|
|
62
|
+
("euc-jp", "EUC-JP", true),
|
|
63
|
+
("euc-jp-ms", "eucJP-ms", true),
|
|
64
|
+
("euc-kr", "EUC-KR", true),
|
|
65
|
+
("euc-tw", "EUC-TW", true),
|
|
66
|
+
("euccn", "GB2312", true),
|
|
67
|
+
("eucjp", "EUC-JP", true),
|
|
68
|
+
("eucjp-ms", "eucJP-ms", true),
|
|
69
|
+
("euckr", "EUC-KR", true),
|
|
70
|
+
("euctw", "EUC-TW", true),
|
|
71
|
+
("gb12345", "GB12345", true),
|
|
72
|
+
("gb18030", "GB18030", true),
|
|
73
|
+
("gb1988", "GB1988", true),
|
|
74
|
+
("gb2312", "GB2312", true),
|
|
75
|
+
("gbk", "GBK", true),
|
|
76
|
+
("ibm037", "IBM037", false),
|
|
77
|
+
("ibm437", "IBM437", true),
|
|
78
|
+
("ibm720", "IBM720", true),
|
|
79
|
+
("ibm737", "IBM737", true),
|
|
80
|
+
("ibm775", "IBM775", true),
|
|
81
|
+
("ibm850", "CP850", true),
|
|
82
|
+
("ibm852", "IBM852", true),
|
|
83
|
+
("ibm855", "IBM855", true),
|
|
84
|
+
("ibm857", "IBM857", true),
|
|
85
|
+
("ibm860", "IBM860", true),
|
|
86
|
+
("ibm861", "IBM861", true),
|
|
87
|
+
("ibm862", "IBM862", true),
|
|
88
|
+
("ibm863", "IBM863", true),
|
|
89
|
+
("ibm864", "IBM864", true),
|
|
90
|
+
("ibm865", "IBM865", true),
|
|
91
|
+
("ibm866", "IBM866", true),
|
|
92
|
+
("ibm869", "IBM869", true),
|
|
93
|
+
("iso-2022-jp", "ISO-2022-JP", false),
|
|
94
|
+
("iso-2022-jp-2", "ISO-2022-JP-2", false),
|
|
95
|
+
("iso-2022-jp-kddi", "ISO-2022-JP-KDDI", false),
|
|
96
|
+
("iso-8859-1", "ISO-8859-1", true),
|
|
97
|
+
("iso-8859-10", "ISO-8859-10", true),
|
|
98
|
+
("iso-8859-11", "ISO-8859-11", true),
|
|
99
|
+
("iso-8859-13", "ISO-8859-13", true),
|
|
100
|
+
("iso-8859-14", "ISO-8859-14", true),
|
|
101
|
+
("iso-8859-15", "ISO-8859-15", true),
|
|
102
|
+
("iso-8859-16", "ISO-8859-16", true),
|
|
103
|
+
("iso-8859-2", "ISO-8859-2", true),
|
|
104
|
+
("iso-8859-3", "ISO-8859-3", true),
|
|
105
|
+
("iso-8859-4", "ISO-8859-4", true),
|
|
106
|
+
("iso-8859-5", "ISO-8859-5", true),
|
|
107
|
+
("iso-8859-6", "ISO-8859-6", true),
|
|
108
|
+
("iso-8859-7", "ISO-8859-7", true),
|
|
109
|
+
("iso-8859-8", "ISO-8859-8", true),
|
|
110
|
+
("iso-8859-9", "ISO-8859-9", true),
|
|
111
|
+
("iso2022-jp", "ISO-2022-JP", false),
|
|
112
|
+
("iso2022-jp2", "ISO-2022-JP-2", false),
|
|
113
|
+
("iso8859-1", "ISO-8859-1", true),
|
|
114
|
+
("iso8859-10", "ISO-8859-10", true),
|
|
115
|
+
("iso8859-11", "ISO-8859-11", true),
|
|
116
|
+
("iso8859-13", "ISO-8859-13", true),
|
|
117
|
+
("iso8859-14", "ISO-8859-14", true),
|
|
118
|
+
("iso8859-15", "ISO-8859-15", true),
|
|
119
|
+
("iso8859-16", "ISO-8859-16", true),
|
|
120
|
+
("iso8859-2", "ISO-8859-2", true),
|
|
121
|
+
("iso8859-3", "ISO-8859-3", true),
|
|
122
|
+
("iso8859-4", "ISO-8859-4", true),
|
|
123
|
+
("iso8859-5", "ISO-8859-5", true),
|
|
124
|
+
("iso8859-6", "ISO-8859-6", true),
|
|
125
|
+
("iso8859-7", "ISO-8859-7", true),
|
|
126
|
+
("iso8859-8", "ISO-8859-8", true),
|
|
127
|
+
("iso8859-9", "ISO-8859-9", true),
|
|
128
|
+
("koi8-r", "KOI8-R", true),
|
|
129
|
+
("koi8-u", "KOI8-U", true),
|
|
130
|
+
("maccenteuro", "macCentEuro", true),
|
|
131
|
+
("maccroatian", "macCroatian", true),
|
|
132
|
+
("maccyrillic", "macCyrillic", true),
|
|
133
|
+
("macgreek", "macGreek", true),
|
|
134
|
+
("maciceland", "macIceland", true),
|
|
135
|
+
("macjapan", "MacJapanese", true),
|
|
136
|
+
("macjapanese", "MacJapanese", true),
|
|
137
|
+
("macroman", "macRoman", true),
|
|
138
|
+
("macromania", "macRomania", true),
|
|
139
|
+
("macthai", "macThai", true),
|
|
140
|
+
("macturkish", "macTurkish", true),
|
|
141
|
+
("macukraine", "macUkraine", true),
|
|
142
|
+
("pck", "Windows-31J", true),
|
|
143
|
+
("shift_jis", "Shift_JIS", true),
|
|
144
|
+
("sjis", "Windows-31J", true),
|
|
145
|
+
("sjis-docomo", "SJIS-DoCoMo", true),
|
|
146
|
+
("sjis-kddi", "SJIS-KDDI", true),
|
|
147
|
+
("sjis-softbank", "SJIS-SoftBank", true),
|
|
148
|
+
("stateless-iso-2022-jp", "stateless-ISO-2022-JP", true),
|
|
149
|
+
("stateless-iso-2022-jp-kddi", "stateless-ISO-2022-JP-KDDI", true),
|
|
150
|
+
("tis-620", "TIS-620", true),
|
|
151
|
+
("ucs-2be", "UTF-16BE", false),
|
|
152
|
+
("ucs-4be", "UTF-32BE", false),
|
|
153
|
+
("ucs-4le", "UTF-32LE", false),
|
|
154
|
+
("us-ascii", "US-ASCII", true),
|
|
155
|
+
("utf-16", "UTF-16", false),
|
|
156
|
+
("utf-16be", "UTF-16BE", false),
|
|
157
|
+
("utf-16le", "UTF-16LE", false),
|
|
158
|
+
("utf-32", "UTF-32", false),
|
|
159
|
+
("utf-32be", "UTF-32BE", false),
|
|
160
|
+
("utf-32le", "UTF-32LE", false),
|
|
161
|
+
("utf-7", "UTF-7", false),
|
|
162
|
+
("utf-8", "UTF-8", true),
|
|
163
|
+
("utf-8-hfs", "UTF8-MAC", true),
|
|
164
|
+
("utf-8-mac", "UTF8-MAC", true),
|
|
165
|
+
("utf8-docomo", "UTF8-DoCoMo", true),
|
|
166
|
+
("utf8-kddi", "UTF8-KDDI", true),
|
|
167
|
+
("utf8-mac", "UTF8-MAC", true),
|
|
168
|
+
("utf8-softbank", "UTF8-SoftBank", true),
|
|
169
|
+
("windows-1250", "Windows-1250", true),
|
|
170
|
+
("windows-1251", "Windows-1251", true),
|
|
171
|
+
("windows-1252", "Windows-1252", true),
|
|
172
|
+
("windows-1253", "Windows-1253", true),
|
|
173
|
+
("windows-1254", "Windows-1254", true),
|
|
174
|
+
("windows-1255", "Windows-1255", true),
|
|
175
|
+
("windows-1256", "Windows-1256", true),
|
|
176
|
+
("windows-1257", "Windows-1257", true),
|
|
177
|
+
("windows-1258", "Windows-1258", true),
|
|
178
|
+
("windows-31j", "Windows-31J", true),
|
|
179
|
+
("windows-874", "Windows-874", true),
|
|
180
|
+
];
|