Disallow unreachable_pub (#4314)

This commit is contained in:
Jonathan Plasse 2023-05-12 00:00:00 +02:00 committed by GitHub
parent 97802e7466
commit c10a4535b9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
444 changed files with 1376 additions and 1106 deletions

View file

@ -116,7 +116,7 @@ impl<'a> Visitor<'a> for AttachmentVisitor {
}
}
pub fn attach(python_cst: &mut [Stmt], trivia: Vec<TriviaToken>) {
pub(crate) fn attach(python_cst: &mut [Stmt], trivia: Vec<TriviaToken>) {
let index = decorate_trivia(trivia, python_cst);
let mut visitor = AttachmentVisitor { index };
for stmt in python_cst {

View file

@ -2,7 +2,7 @@ use ruff_python_ast::source_code::Locator;
use ruff_text_size::{TextLen, TextRange, TextSize};
/// Return `true` if the given string is a radix literal (e.g., `0b101`).
pub fn is_radix_literal(content: &str) -> bool {
pub(crate) fn is_radix_literal(content: &str) -> bool {
content.starts_with("0b")
|| content.starts_with("0o")
|| content.starts_with("0x")
@ -12,7 +12,7 @@ pub fn is_radix_literal(content: &str) -> bool {
}
/// Find the first token in the given range that satisfies the given predicate.
pub fn find_tok(
pub(crate) fn find_tok(
range: TextRange,
locator: &Locator,
f: impl Fn(rustpython_parser::Tok) -> bool,
@ -35,7 +35,7 @@ pub fn find_tok(
///
/// `location` is the start of the compound statement (e.g., the `if` in `if x:`).
/// `end_location` is the end of the last statement in the body.
pub fn expand_indented_block(
pub(crate) fn expand_indented_block(
location: TextSize,
end_location: TextSize,
locator: &Locator,
@ -126,7 +126,7 @@ pub fn expand_indented_block(
}
/// Return true if the `orelse` block of an `if` statement is an `elif` statement.
pub fn is_elif(orelse: &[rustpython_parser::ast::Stmt], locator: &Locator) -> bool {
pub(crate) fn is_elif(orelse: &[rustpython_parser::ast::Stmt], locator: &Locator) -> bool {
if orelse.len() == 1 && matches!(orelse[0].node, rustpython_parser::ast::StmtKind::If { .. }) {
let contents = locator.after(orelse[0].start());
if contents.starts_with("elif") {

View file

@ -13,8 +13,8 @@ use ruff_python_ast::source_code::Locator;
use crate::cst::helpers::{expand_indented_block, find_tok, is_elif};
use crate::trivia::{Parenthesize, Trivia};
pub mod helpers;
pub mod visitor;
pub(crate) mod helpers;
pub(crate) mod visitor;
type Ident = String;
@ -96,7 +96,7 @@ impl From<&rustpython_parser::ast::Boolop> for BoolOpKind {
}
}
pub type BoolOp = Attributed<BoolOpKind>;
pub(crate) type BoolOp = Attributed<BoolOpKind>;
#[derive(Clone, Debug, PartialEq)]
pub enum OperatorKind {
@ -115,7 +115,7 @@ pub enum OperatorKind {
FloorDiv,
}
pub type Operator = Attributed<OperatorKind>;
pub(crate) type Operator = Attributed<OperatorKind>;
impl From<&rustpython_parser::ast::Operator> for OperatorKind {
fn from(op: &rustpython_parser::ast::Operator) -> Self {
@ -145,7 +145,7 @@ pub enum UnaryOpKind {
USub,
}
pub type UnaryOp = Attributed<UnaryOpKind>;
pub(crate) type UnaryOp = Attributed<UnaryOpKind>;
impl From<&rustpython_parser::ast::Unaryop> for UnaryOpKind {
fn from(op: &rustpython_parser::ast::Unaryop) -> Self {
@ -172,7 +172,7 @@ pub enum CmpOpKind {
NotIn,
}
pub type CmpOp = Attributed<CmpOpKind>;
pub(crate) type CmpOp = Attributed<CmpOpKind>;
impl From<&rustpython_parser::ast::Cmpop> for CmpOpKind {
fn from(op: &rustpython_parser::ast::Cmpop) -> Self {
@ -191,7 +191,7 @@ impl From<&rustpython_parser::ast::Cmpop> for CmpOpKind {
}
}
pub type Body = Attributed<Vec<Stmt>>;
pub(crate) type Body = Attributed<Vec<Stmt>>;
impl From<(Vec<rustpython_parser::ast::Stmt>, &Locator<'_>)> for Body {
fn from((body, locator): (Vec<rustpython_parser::ast::Stmt>, &Locator)) -> Self {
@ -335,7 +335,7 @@ pub enum StmtKind {
Continue,
}
pub type Stmt = Attributed<StmtKind>;
pub(crate) type Stmt = Attributed<StmtKind>;
#[derive(Clone, Debug, PartialEq)]
pub enum ExprKind {
@ -453,7 +453,7 @@ pub enum ExprKind {
},
}
pub type Expr = Attributed<ExprKind>;
pub(crate) type Expr = Attributed<ExprKind>;
#[derive(Clone, Debug, PartialEq)]
pub struct Comprehension {
@ -472,7 +472,7 @@ pub enum ExcepthandlerKind {
},
}
pub type Excepthandler = Attributed<ExcepthandlerKind>;
pub(crate) type Excepthandler = Attributed<ExcepthandlerKind>;
#[derive(Clone, Debug, PartialEq)]
pub enum SliceIndexKind {
@ -482,7 +482,7 @@ pub enum SliceIndexKind {
Index { value: Box<Expr> },
}
pub type SliceIndex = Attributed<SliceIndexKind>;
pub(crate) type SliceIndex = Attributed<SliceIndexKind>;
#[derive(Clone, Debug, PartialEq)]
pub struct Arguments {
@ -502,7 +502,7 @@ pub struct ArgData {
pub type_comment: Option<String>,
}
pub type Arg = Attributed<ArgData>;
pub(crate) type Arg = Attributed<ArgData>;
#[derive(Clone, Debug, PartialEq)]
pub struct KeywordData {
@ -510,7 +510,7 @@ pub struct KeywordData {
pub value: Expr,
}
pub type Keyword = Attributed<KeywordData>;
pub(crate) type Keyword = Attributed<KeywordData>;
#[derive(Clone, Debug, PartialEq)]
pub struct AliasData {
@ -518,7 +518,7 @@ pub struct AliasData {
pub asname: Option<Ident>,
}
pub type Alias = Attributed<AliasData>;
pub(crate) type Alias = Attributed<AliasData>;
#[derive(Clone, Debug, PartialEq)]
pub struct Withitem {
@ -568,7 +568,7 @@ pub enum PatternKind {
},
}
pub type Pattern = Attributed<PatternKind>;
pub(crate) type Pattern = Attributed<PatternKind>;
impl From<(rustpython_parser::ast::Alias, &Locator<'_>)> for Alias {
fn from((alias, _locator): (rustpython_parser::ast::Alias, &Locator)) -> Self {

View file

@ -6,7 +6,7 @@ use crate::cst::{
SliceIndexKind, Stmt, StmtKind, UnaryOp, Withitem,
};
pub trait Visitor<'a> {
pub(crate) trait Visitor<'a> {
fn visit_stmt(&mut self, stmt: &'a mut Stmt) {
walk_stmt(self, stmt);
}
@ -72,13 +72,13 @@ pub trait Visitor<'a> {
}
}
pub fn walk_body<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, body: &'a mut Body) {
pub(crate) fn walk_body<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, body: &'a mut Body) {
for stmt in &mut body.node {
visitor.visit_stmt(stmt);
}
}
pub fn walk_stmt<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, stmt: &'a mut Stmt) {
pub(crate) fn walk_stmt<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, stmt: &'a mut Stmt) {
match &mut stmt.node {
StmtKind::FunctionDef {
args,
@ -292,7 +292,7 @@ pub fn walk_stmt<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, stmt: &'a mut Stm
}
}
pub fn walk_expr<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, expr: &'a mut Expr) {
pub(crate) fn walk_expr<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, expr: &'a mut Expr) {
match &mut expr.node {
ExprKind::BoolOp { ops, values } => {
for op in ops {
@ -451,7 +451,10 @@ pub fn walk_expr<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, expr: &'a mut Exp
}
}
pub fn walk_constant<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, constant: &'a mut Constant) {
pub(crate) fn walk_constant<'a, V: Visitor<'a> + ?Sized>(
visitor: &mut V,
constant: &'a mut Constant,
) {
if let Constant::Tuple(constants) = constant {
for constant in constants {
visitor.visit_constant(constant);
@ -459,7 +462,7 @@ pub fn walk_constant<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, constant: &'a
}
}
pub fn walk_comprehension<'a, V: Visitor<'a> + ?Sized>(
pub(crate) fn walk_comprehension<'a, V: Visitor<'a> + ?Sized>(
visitor: &mut V,
comprehension: &'a mut Comprehension,
) {
@ -470,7 +473,7 @@ pub fn walk_comprehension<'a, V: Visitor<'a> + ?Sized>(
}
}
pub fn walk_excepthandler<'a, V: Visitor<'a> + ?Sized>(
pub(crate) fn walk_excepthandler<'a, V: Visitor<'a> + ?Sized>(
visitor: &mut V,
excepthandler: &'a mut Excepthandler,
) {
@ -484,7 +487,7 @@ pub fn walk_excepthandler<'a, V: Visitor<'a> + ?Sized>(
}
}
pub fn walk_slice_index<'a, V: Visitor<'a> + ?Sized>(
pub(crate) fn walk_slice_index<'a, V: Visitor<'a> + ?Sized>(
visitor: &mut V,
slice_index: &'a mut SliceIndex,
) {
@ -496,7 +499,10 @@ pub fn walk_slice_index<'a, V: Visitor<'a> + ?Sized>(
}
}
pub fn walk_arguments<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, arguments: &'a mut Arguments) {
pub(crate) fn walk_arguments<'a, V: Visitor<'a> + ?Sized>(
visitor: &mut V,
arguments: &'a mut Arguments,
) {
for arg in &mut arguments.posonlyargs {
visitor.visit_arg(arg);
}
@ -520,24 +526,27 @@ pub fn walk_arguments<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, arguments: &
}
}
pub fn walk_arg<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, arg: &'a mut Arg) {
pub(crate) fn walk_arg<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, arg: &'a mut Arg) {
if let Some(expr) = &mut arg.node.annotation {
visitor.visit_annotation(expr);
}
}
pub fn walk_keyword<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, keyword: &'a mut Keyword) {
pub(crate) fn walk_keyword<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, keyword: &'a mut Keyword) {
visitor.visit_expr(&mut keyword.node.value);
}
pub fn walk_withitem<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, withitem: &'a mut Withitem) {
pub(crate) fn walk_withitem<'a, V: Visitor<'a> + ?Sized>(
visitor: &mut V,
withitem: &'a mut Withitem,
) {
visitor.visit_expr(&mut withitem.context_expr);
if let Some(expr) = &mut withitem.optional_vars {
visitor.visit_expr(expr);
}
}
pub fn walk_match_case<'a, V: Visitor<'a> + ?Sized>(
pub(crate) fn walk_match_case<'a, V: Visitor<'a> + ?Sized>(
visitor: &mut V,
match_case: &'a mut MatchCase,
) {
@ -548,7 +557,7 @@ pub fn walk_match_case<'a, V: Visitor<'a> + ?Sized>(
visitor.visit_body(&mut match_case.body);
}
pub fn walk_pattern<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, pattern: &'a mut Pattern) {
pub(crate) fn walk_pattern<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, pattern: &'a mut Pattern) {
match &mut pattern.node {
PatternKind::MatchValue { value } => visitor.visit_expr(value),
PatternKind::MatchSingleton { value } => visitor.visit_constant(value),
@ -595,23 +604,31 @@ pub fn walk_pattern<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, pattern: &'a m
}
#[allow(unused_variables)]
pub fn walk_expr_context<'a, V: Visitor<'a> + ?Sized>(
pub(crate) fn walk_expr_context<'a, V: Visitor<'a> + ?Sized>(
visitor: &mut V,
expr_context: &'a mut ExprContext,
) {
}
#[allow(unused_variables)]
pub fn walk_bool_op<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, bool_op: &'a mut BoolOp) {}
pub(crate) fn walk_bool_op<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, bool_op: &'a mut BoolOp) {}
#[allow(unused_variables)]
pub fn walk_operator<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, operator: &'a mut Operator) {}
pub(crate) fn walk_operator<'a, V: Visitor<'a> + ?Sized>(
visitor: &mut V,
operator: &'a mut Operator,
) {
}
#[allow(unused_variables)]
pub fn walk_unary_op<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, unary_op: &'a mut UnaryOp) {}
pub(crate) fn walk_unary_op<'a, V: Visitor<'a> + ?Sized>(
visitor: &mut V,
unary_op: &'a mut UnaryOp,
) {
}
#[allow(unused_variables)]
pub fn walk_cmp_op<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, cmp_op: &'a mut CmpOp) {}
pub(crate) fn walk_cmp_op<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, cmp_op: &'a mut CmpOp) {}
#[allow(unused_variables)]
pub fn walk_alias<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, alias: &'a mut Alias) {}
pub(crate) fn walk_alias<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, alias: &'a mut Alias) {}

View file

@ -8,7 +8,7 @@ use crate::shared_traits::AsFormat;
use crate::trivia::{Relationship, TriviaKind};
#[derive(Copy, Clone)]
pub struct Block<'a> {
pub(crate) struct Block<'a> {
body: &'a Body,
}
@ -40,12 +40,12 @@ impl Format<ASTFormatContext> for Block<'_> {
}
#[inline]
pub fn block(body: &Body) -> Block {
pub(crate) fn block(body: &Body) -> Block {
Block { body }
}
#[derive(Copy, Clone)]
pub struct Statements<'a> {
pub(crate) struct Statements<'a> {
suite: &'a [Stmt],
}
@ -61,12 +61,12 @@ impl Format<ASTFormatContext> for Statements<'_> {
}
}
pub fn statements(suite: &[Stmt]) -> Statements {
pub(crate) fn statements(suite: &[Stmt]) -> Statements {
Statements { suite }
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct Literal {
pub(crate) struct Literal {
range: TextRange,
}
@ -82,7 +82,7 @@ impl Format<ASTFormatContext> for Literal {
}
#[inline]
pub const fn literal(range: TextRange) -> Literal {
pub(crate) const fn literal(range: TextRange) -> Literal {
Literal { range }
}

View file

@ -7,7 +7,7 @@ use crate::format::builders::literal;
use crate::trivia::TriviaKind;
#[derive(Debug)]
pub struct LeadingComments<'a, T> {
pub(crate) struct LeadingComments<'a, T> {
item: &'a Attributed<T>,
}
@ -31,12 +31,12 @@ impl<T> Format<ASTFormatContext> for LeadingComments<'_, T> {
}
#[inline]
pub const fn leading_comments<T>(item: &Attributed<T>) -> LeadingComments<'_, T> {
pub(crate) const fn leading_comments<T>(item: &Attributed<T>) -> LeadingComments<'_, T> {
LeadingComments { item }
}
#[derive(Debug)]
pub struct TrailingComments<'a, T> {
pub(crate) struct TrailingComments<'a, T> {
item: &'a Attributed<T>,
}
@ -60,12 +60,12 @@ impl<T> Format<ASTFormatContext> for TrailingComments<'_, T> {
}
#[inline]
pub const fn trailing_comments<T>(item: &Attributed<T>) -> TrailingComments<'_, T> {
pub(crate) const fn trailing_comments<T>(item: &Attributed<T>) -> TrailingComments<'_, T> {
TrailingComments { item }
}
#[derive(Debug)]
pub struct EndOfLineComments<'a, T> {
pub(crate) struct EndOfLineComments<'a, T> {
item: &'a Attributed<T>,
}
@ -88,12 +88,12 @@ impl<T> Format<ASTFormatContext> for EndOfLineComments<'_, T> {
}
#[inline]
pub const fn end_of_line_comments<T>(item: &Attributed<T>) -> EndOfLineComments<'_, T> {
pub(crate) const fn end_of_line_comments<T>(item: &Attributed<T>) -> EndOfLineComments<'_, T> {
EndOfLineComments { item }
}
#[derive(Debug)]
pub struct DanglingComments<'a, T> {
pub(crate) struct DanglingComments<'a, T> {
item: &'a Attributed<T>,
}
@ -113,6 +113,6 @@ impl<T> Format<ASTFormatContext> for DanglingComments<'_, T> {
}
#[inline]
pub const fn dangling_comments<T>(item: &Attributed<T>) -> DanglingComments<'_, T> {
pub(crate) const fn dangling_comments<T>(item: &Attributed<T>) -> DanglingComments<'_, T> {
DanglingComments { item }
}

View file

@ -1,6 +1,6 @@
use crate::cst::{Expr, ExprKind, UnaryOpKind};
pub fn is_self_closing(expr: &Expr) -> bool {
pub(crate) fn is_self_closing(expr: &Expr) -> bool {
match &expr.node {
ExprKind::Tuple { .. }
| ExprKind::List { .. }
@ -53,7 +53,7 @@ pub fn is_self_closing(expr: &Expr) -> bool {
/// Return `true` if an [`Expr`] adheres to Black's definition of a non-complex
/// expression, in the context of a slice operation.
pub fn is_simple_slice(expr: &Expr) -> bool {
pub(crate) fn is_simple_slice(expr: &Expr) -> bool {
match &expr.node {
ExprKind::UnaryOp { op, operand } => {
if matches!(op.node, UnaryOpKind::Not) {
@ -70,7 +70,7 @@ pub fn is_simple_slice(expr: &Expr) -> bool {
/// Return `true` if an [`Expr`] adheres to Black's definition of a non-complex
/// expression, in the context of a power operation.
pub fn is_simple_power(expr: &Expr) -> bool {
pub(crate) fn is_simple_power(expr: &Expr) -> bool {
match &expr.node {
ExprKind::UnaryOp { op, operand } => {
if matches!(op.node, UnaryOpKind::Not) {

View file

@ -2,7 +2,7 @@ mod alias;
mod arg;
mod arguments;
mod bool_op;
pub mod builders;
pub(crate) mod builders;
mod cmp_op;
mod comments;
mod comprehension;

View file

@ -64,7 +64,7 @@ const fn float_atom(range: TextRange) -> FloatAtom {
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct FloatLiteral {
pub(crate) struct FloatLiteral {
range: TextRange,
}
@ -109,12 +109,12 @@ impl Format<ASTFormatContext> for FloatLiteral {
}
#[inline]
pub const fn float_literal(range: TextRange) -> FloatLiteral {
pub(crate) const fn float_literal(range: TextRange) -> FloatLiteral {
FloatLiteral { range }
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct IntLiteral {
pub(crate) struct IntLiteral {
range: TextRange,
}
@ -156,12 +156,12 @@ impl Format<ASTFormatContext> for IntLiteral {
}
#[inline]
pub const fn int_literal(range: TextRange) -> IntLiteral {
pub(crate) const fn int_literal(range: TextRange) -> IntLiteral {
IntLiteral { range }
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct ComplexLiteral {
pub(crate) struct ComplexLiteral {
range: TextRange,
}
@ -190,6 +190,6 @@ impl Format<ASTFormatContext> for ComplexLiteral {
}
#[inline]
pub const fn complex_literal(range: TextRange) -> ComplexLiteral {
pub(crate) const fn complex_literal(range: TextRange) -> ComplexLiteral {
ComplexLiteral { range }
}

View file

@ -9,7 +9,7 @@ use crate::context::ASTFormatContext;
use crate::cst::Expr;
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct StringLiteralPart {
pub(crate) struct StringLiteralPart {
range: TextRange,
}
@ -110,12 +110,12 @@ impl Format<ASTFormatContext> for StringLiteralPart {
}
#[inline]
pub const fn string_literal_part(range: TextRange) -> StringLiteralPart {
pub(crate) const fn string_literal_part(range: TextRange) -> StringLiteralPart {
StringLiteralPart { range }
}
#[derive(Debug, Copy, Clone)]
pub struct StringLiteral<'a> {
pub(crate) struct StringLiteral<'a> {
expr: &'a Expr,
}
@ -159,7 +159,7 @@ impl Format<ASTFormatContext> for StringLiteral<'_> {
}
#[inline]
pub const fn string_literal(expr: &Expr) -> StringLiteral {
pub(crate) const fn string_literal(expr: &Expr) -> StringLiteral {
StringLiteral { expr }
}

View file

@ -353,7 +353,7 @@ impl<'a> Visitor<'a> for ExprNormalizer {
}
}
pub fn normalize_newlines(python_cst: &mut [Stmt]) {
pub(crate) fn normalize_newlines(python_cst: &mut [Stmt]) {
let mut normalizer = StmtNormalizer {
depth: Depth::TopLevel,
trailer: Trailer::None,

View file

@ -191,7 +191,7 @@ impl<'a> Visitor<'a> for ParenthesesNormalizer<'_> {
///
/// TODO(charlie): It's weird that we have both `TriviaKind::Parentheses` (which aren't used
/// during formatting) and `Parenthesize` (which are used during formatting).
pub fn normalize_parentheses(python_cst: &mut [Stmt], locator: &Locator) {
pub(crate) fn normalize_parentheses(python_cst: &mut [Stmt], locator: &Locator) {
let mut normalizer = ParenthesesNormalizer { locator };
for stmt in python_cst {
normalizer.visit_stmt(stmt);