Remove allow(pedantic) from formatter (#6549)

This commit is contained in:
Micha Reiser 2023-08-14 14:02:06 +02:00 committed by GitHub
parent c39bcbadff
commit 9584f613b9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
25 changed files with 348 additions and 316 deletions

View file

@ -1,15 +1,15 @@
//! Infrastructure for code formatting
//!
//! This module defines [FormatElement], an IR to format code documents and provides a mean to print
//! This module defines [`FormatElement`], an IR to format code documents and provides a mean to print
//! such a document to a string. Objects that know how to format themselves implement the [Format] trait.
//!
//! ## Formatting Traits
//!
//! * [Format]: Implemented by objects that can be formatted.
//! * [FormatRule]: Rule that knows how to format an object of another type. Necessary in the situation where
//! * [`FormatRule`]: Rule that knows how to format an object of another type. Necessary in the situation where
//! it's necessary to implement [Format] on an object from another crate. This module defines the
//! [FormatRefWithRule] and [FormatOwnedWithRule] structs to pass an item with its corresponding rule.
//! * [FormatWithRule] implemented by objects that know how to format another type. Useful for implementing
//! [`FormatRefWithRule`] and [`FormatOwnedWithRule`] structs to pass an item with its corresponding rule.
//! * [`FormatWithRule`] implemented by objects that know how to format another type. Useful for implementing
//! some reusable formatting logic inside of this module if the type itself doesn't implement [Format]
//!
//! ## Formatting Macros
@ -19,9 +19,6 @@
//! * [`format_args!`]: Concatenates a sequence of Format objects.
//! * [`write!`]: Writes a sequence of formatable objects into an output buffer.
#![allow(clippy::pedantic, unsafe_code)]
#![deny(rustdoc::broken_intra_doc_links)]
mod arguments;
mod buffer;
mod builders;
@ -73,12 +70,12 @@ pub enum IndentStyle {
impl IndentStyle {
pub const DEFAULT_SPACES: u8 = 2;
/// Returns `true` if this is an [IndentStyle::Tab].
/// Returns `true` if this is an [`IndentStyle::Tab`].
pub const fn is_tab(&self) -> bool {
matches!(self, IndentStyle::Tab)
}
/// Returns `true` if this is an [IndentStyle::Space].
/// Returns `true` if this is an [`IndentStyle::Space`].
pub const fn is_space(&self) -> bool {
matches!(self, IndentStyle::Space(_))
}
@ -121,10 +118,10 @@ impl std::fmt::Display for IndentStyle {
pub struct LineWidth(u16);
impl LineWidth {
/// Maximum allowed value for a valid [LineWidth]
/// Maximum allowed value for a valid [`LineWidth`]
pub const MAX: u16 = 320;
/// Return the numeric value for this [LineWidth]
/// Return the numeric value for this [`LineWidth`]
pub fn value(&self) -> u16 {
self.0
}
@ -136,7 +133,7 @@ impl Default for LineWidth {
}
}
/// Error type returned when parsing a [LineWidth] from a string fails
/// Error type returned when parsing a [`LineWidth`] from a string fails
pub enum ParseLineWidthError {
/// The string could not be parsed as a valid [u16]
ParseError(ParseIntError),
@ -169,7 +166,7 @@ impl FromStr for LineWidth {
}
}
/// Error type returned when converting a u16 to a [LineWidth] fails
/// Error type returned when converting a u16 to a [`LineWidth`] fails
#[derive(Clone, Copy, Debug)]
pub struct LineWidthFromIntError(pub u16);
@ -238,6 +235,7 @@ impl SimpleFormatContext {
}
}
#[must_use]
pub fn with_source_code(mut self, code: &str) -> Self {
self.source_code = String::from(code);
self
@ -390,20 +388,20 @@ impl Printed {
self.range
}
/// Returns a list of [SourceMarker] mapping byte positions
/// Returns a list of [`SourceMarker`] mapping byte positions
/// in the output string to the input source code.
/// It's not guaranteed that the markers are sorted by source position.
pub fn sourcemap(&self) -> &[SourceMarker] {
&self.sourcemap
}
/// Returns a list of [SourceMarker] mapping byte positions
/// Returns a list of [`SourceMarker`] mapping byte positions
/// in the output string to the input source code, consuming the result
pub fn into_sourcemap(self) -> Vec<SourceMarker> {
self.sourcemap
}
/// Takes the list of [SourceMarker] mapping byte positions in the output string
/// Takes the list of [`SourceMarker`] mapping byte positions in the output string
/// to the input source code.
pub fn take_sourcemap(&mut self) -> Vec<SourceMarker> {
std::mem::take(&mut self.sourcemap)
@ -441,7 +439,7 @@ impl Printed {
pub type FormatResult<F> = Result<F, FormatError>;
/// Formatting trait for types that can create a formatted representation. The `ruff_formatter` equivalent
/// to [std::fmt::Display].
/// to [`std::fmt::Display`].
///
/// ## Example
/// Implementing `Format` for a custom struct
@ -480,7 +478,7 @@ impl<T, Context> Format<Context> for &T
where
T: ?Sized + Format<Context>,
{
#[inline(always)]
#[inline]
fn fmt(&self, f: &mut Formatter<Context>) -> FormatResult<()> {
Format::fmt(&**self, f)
}
@ -490,7 +488,7 @@ impl<T, Context> Format<Context> for &mut T
where
T: ?Sized + Format<Context>,
{
#[inline(always)]
#[inline]
fn fmt(&self, f: &mut Formatter<Context>) -> FormatResult<()> {
Format::fmt(&**self, f)
}
@ -518,7 +516,7 @@ impl<Context> Format<Context> for () {
/// Rule that knows how to format an object of type `T`.
///
/// Implementing [Format] on the object itself is preferred over implementing [FormatRule] but
/// Implementing [Format] on the object itself is preferred over implementing [`FormatRule`] but
/// this isn't possible inside of a dependent crate for external type.
///
/// For example, the `ruff_js_formatter` crate isn't able to implement [Format] on `JsIfStatement`
@ -535,6 +533,7 @@ pub trait FormatRuleWithOptions<T, C>: FormatRule<T, C> {
type Options;
/// Returns a new rule that uses the given options to format an object.
#[must_use]
fn with_options(self, options: Self::Options) -> Self;
}
@ -547,9 +546,9 @@ pub trait FormatRuleWithOptions<T, C>: FormatRule<T, C> {
///
/// ## Examples
///
/// This can be useful if you want to format a `SyntaxNode` inside ruff_formatter.. `SyntaxNode` doesn't implement [Format]
/// This can be useful if you want to format a `SyntaxNode` inside `ruff_formatter`.. `SyntaxNode` doesn't implement [Format]
/// itself but the language specific crate implements `AsFormat` and `IntoFormat` for it and the returned [Format]
/// implement [FormatWithRule].
/// implement [`FormatWithRule`].
///
/// ```ignore
/// use ruff_formatter::prelude::*;
@ -597,6 +596,7 @@ impl<T, R, O, C> FormatRefWithRule<'_, T, R, C>
where
R: FormatRuleWithOptions<T, C, Options = O>,
{
#[must_use]
pub fn with_options(mut self, options: O) -> Self {
self.rule = self.rule.with_options(options);
self
@ -618,7 +618,7 @@ impl<T, R, C> Format<C> for FormatRefWithRule<'_, T, R, C>
where
R: FormatRule<T, C>,
{
#[inline(always)]
#[inline]
fn fmt(&self, f: &mut Formatter<C>) -> FormatResult<()> {
self.rule.fmt(self.item, f)
}
@ -647,6 +647,7 @@ where
}
}
#[must_use]
pub fn with_item(mut self, item: T) -> Self {
self.item = item;
self
@ -661,7 +662,7 @@ impl<T, R, C> Format<C> for FormatOwnedWithRule<T, R, C>
where
R: FormatRule<T, C>,
{
#[inline(always)]
#[inline]
fn fmt(&self, f: &mut Formatter<C>) -> FormatResult<()> {
self.rule.fmt(&self.item, f)
}
@ -671,6 +672,7 @@ impl<T, R, O, C> FormatOwnedWithRule<T, R, C>
where
R: FormatRuleWithOptions<T, C, Options = O>,
{
#[must_use]
pub fn with_options(mut self, options: O) -> Self {
self.rule = self.rule.with_options(options);
self
@ -729,7 +731,7 @@ where
/// # Ok(())
/// # }
/// ```
#[inline(always)]
#[inline]
pub fn write<Context>(
output: &mut dyn Buffer<Context = Context>,
args: Arguments<Context>,
@ -790,9 +792,9 @@ where
/// This structure stores the state that is relevant for the formatting of the whole document.
///
/// This structure is different from [crate::Formatter] in that the formatting infrastructure
/// creates a new [crate::Formatter] for every [crate::write!] call, whereas this structure stays alive
/// for the whole process of formatting a root with [crate::format!].
/// This structure is different from [`crate::Formatter`] in that the formatting infrastructure
/// creates a new [`crate::Formatter`] for every [`crate::write`!] call, whereas this structure stays alive
/// for the whole process of formatting a root with [`crate::format`!].
pub struct FormatState<Context> {
context: Context,
@ -815,7 +817,7 @@ impl<Context> FormatState<Context> {
pub fn new(context: Context) -> Self {
Self {
context,
group_id_builder: Default::default(),
group_id_builder: UniqueGroupIdBuilder::default(),
}
}
@ -834,7 +836,7 @@ impl<Context> FormatState<Context> {
}
/// Creates a new group id that is unique to this document. The passed debug name is used in the
/// [std::fmt::Debug] of the document if this is a debug build.
/// [`std::fmt::Debug`] of the document if this is a debug build.
/// The name is unused for production builds and has no meaning on the equality of two group ids.
pub fn group_id(&self, debug_name: &'static str) -> GroupId {
self.group_id_builder.group_id(debug_name)