rrubocop 0.8.15

10x faster RuboCop drop-in. Same output format and configs, without maintaining RuboCop gems.
//! Detect whether `self.` is redundant on a call.

use tree_sitter::Node;

use crate::cop::shared::call_method_name;
use crate::parse::source::SourceFile;

use super::shadow::name_is_shadowed;

pub(super) fn self_is_redundant(source: &SourceFile, node: Node<'_>) -> bool {
    let Some(method) = call_method_name(source, node) else {
        return false;
    };
    if excluded_method(method) {
        return false;
    }
    !name_is_shadowed(source, node, method)
}

fn excluded_method(method: &[u8]) -> bool {
    method == b"class"
        || method.starts_with(b"[")
        || method.first().is_some_and(|b| b.is_ascii_uppercase())
        || is_setter_name(method)
        || !is_simple_method_name(method)
}

fn is_setter_name(method: &[u8]) -> bool {
    method.ends_with(b"=") && method != b"==" && method != b"!=" && method != b"=~" && method != b"!~"
}

fn is_simple_method_name(method: &[u8]) -> bool {
    method
        .iter()
        .all(|b| b.is_ascii_alphanumeric() || matches!(*b, b'_' | b'!' | b'?' | b'='))
}