perf: clippy::missing_const_for_fn (#974)

This commit is contained in:
EdJoPaTo 2024-02-27 12:32:07 +01:00 committed by Josh McKinney
parent 27680c05ce
commit 3834374652
No known key found for this signature in database
GPG key ID: 722287396A903BC5
25 changed files with 71 additions and 65 deletions

View file

@ -65,6 +65,7 @@ serde_json = "1.0.109"
unsafe_code = "forbid"
[lints.clippy]
explicit_iter_loop = "warn"
missing_const_for_fn = "warn"
needless_for_each = "warn"
semicolon_if_nothing_returned = "warn"

View file

@ -97,7 +97,7 @@ where
/// # use ratatui::prelude::*;
/// let backend = CrosstermBackend::new(stdout());
/// ```
pub fn new(writer: W) -> CrosstermBackend<W> {
pub const fn new(writer: W) -> CrosstermBackend<W> {
CrosstermBackend { writer }
}
}

View file

@ -82,7 +82,7 @@ where
/// # use ratatui::prelude::*;
/// let backend = TermionBackend::new(stdout());
/// ```
pub fn new(writer: W) -> TermionBackend<W> {
pub const fn new(writer: W) -> TermionBackend<W> {
TermionBackend { writer }
}
}

View file

@ -93,14 +93,16 @@ impl TermwizBackend {
}
/// Creates a new Termwiz backend instance with the given buffered terminal.
pub fn with_buffered_terminal(instance: BufferedTerminal<SystemTerminal>) -> TermwizBackend {
pub const fn with_buffered_terminal(
instance: BufferedTerminal<SystemTerminal>,
) -> TermwizBackend {
TermwizBackend {
buffered_terminal: instance,
}
}
/// Returns a reference to the buffered terminal used by the backend.
pub fn buffered_terminal(&self) -> &BufferedTerminal<SystemTerminal> {
pub const fn buffered_terminal(&self) -> &BufferedTerminal<SystemTerminal> {
&self.buffered_terminal
}

View file

@ -84,7 +84,7 @@ impl TestBackend {
}
/// Returns a reference to the internal buffer of the TestBackend.
pub fn buffer(&self) -> &Buffer {
pub const fn buffer(&self) -> &Buffer {
&self.buffer
}

View file

@ -92,7 +92,7 @@ impl Buffer {
}
/// Returns the area covered by this buffer
pub fn area(&self) -> &Rect {
pub const fn area(&self) -> &Rect {
&self.area
}

View file

@ -49,7 +49,7 @@ impl Frame<'_> {
/// If your app listens for a resize event from the backend, it should ignore the values from
/// the event for any calculations that are used to render the current frame and use this value
/// instead as this is the size of the buffer that is used to render the current frame.
pub fn size(&self) -> Rect {
pub const fn size(&self) -> Rect {
self.viewport_area
}
@ -193,7 +193,7 @@ impl Frame<'_> {
/// let current_count = frame.count();
/// println!("Current frame count: {}", current_count);
/// ```
pub fn count(&self) -> usize {
pub const fn count(&self) -> usize {
self.count
}
}

View file

@ -172,7 +172,7 @@ where
}
/// Gets the backend
pub fn backend(&self) -> &B {
pub const fn backend(&self) -> &B {
&self.backend
}

View file

@ -36,7 +36,7 @@ impl<'a> Masked<'a> {
}
/// The character to use for masking.
pub fn mask_char(&self) -> char {
pub const fn mask_char(&self) -> char {
self.mask_char
}

View file

@ -163,7 +163,7 @@ impl<'a> BarChart<'a> {
/// // f b b
/// ```
#[must_use = "method moves the value of self and returns the modified value"]
pub fn max(mut self, max: u64) -> BarChart<'a> {
pub const fn max(mut self, max: u64) -> BarChart<'a> {
self.max = Some(max);
self
}
@ -213,7 +213,7 @@ impl<'a> BarChart<'a> {
/// // f b
/// ```
#[must_use = "method moves the value of self and returns the modified value"]
pub fn bar_gap(mut self, gap: u16) -> BarChart<'a> {
pub const fn bar_gap(mut self, gap: u16) -> BarChart<'a> {
self.bar_gap = gap;
self
}
@ -222,7 +222,7 @@ impl<'a> BarChart<'a> {
///
/// If not set, the default is [`bar::NINE_LEVELS`](crate::symbols::bar::NINE_LEVELS).
#[must_use = "method moves the value of self and returns the modified value"]
pub fn bar_set(mut self, bar_set: symbols::bar::Set) -> BarChart<'a> {
pub const fn bar_set(mut self, bar_set: symbols::bar::Set) -> BarChart<'a> {
self.bar_set = bar_set;
self
}
@ -263,7 +263,7 @@ impl<'a> BarChart<'a> {
/// Set the gap between [`BarGroup`].
#[must_use = "method moves the value of self and returns the modified value"]
pub fn group_gap(mut self, gap: u16) -> BarChart<'a> {
pub const fn group_gap(mut self, gap: u16) -> BarChart<'a> {
self.group_gap = gap;
self
}
@ -300,7 +300,7 @@ impl<'a> BarChart<'a> {
/// █bar██
/// ```
#[must_use = "method moves the value of self and returns the modified value"]
pub fn direction(mut self, direction: Direction) -> BarChart<'a> {
pub const fn direction(mut self, direction: Direction) -> BarChart<'a> {
self.direction = direction;
self
}

View file

@ -50,7 +50,7 @@ impl<'a> Bar<'a> {
/// [`Bar::value_style`] to style the value.
/// [`Bar::text_value`] to set the displayed value.
#[must_use = "method moves the value of self and returns the modified value"]
pub fn value(mut self, value: u64) -> Bar<'a> {
pub const fn value(mut self, value: u64) -> Bar<'a> {
self.value = value;
self
}

View file

@ -1107,7 +1107,7 @@ mod tests {
}
#[test]
fn border_type_can_be_const() {
const fn border_type_can_be_const() {
const _PLAIN: border::Set = BorderType::border_symbols(BorderType::Plain);
}
@ -1130,7 +1130,7 @@ mod tests {
}
#[test]
fn block_can_be_const() {
const fn block_can_be_const() {
const _DEFAULT_STYLE: Style = Style::new();
const _DEFAULT_PADDING: Padding = Padding::uniform(1);
const _DEFAULT_BLOCK: Block = Block::new()

View file

@ -186,7 +186,7 @@ mod tests {
}
#[test]
fn can_be_const() {
const fn can_be_const() {
const _PADDING: Padding = Padding::new(1, 1, 1, 1);
const _UNI_PADDING: Padding = Padding::uniform(1);
const _NO_PADDING: Padding = Padding::zero();

View file

@ -97,14 +97,14 @@ impl<'a> Title<'a> {
/// Set the title alignment.
#[must_use = "method moves the value of self and returns the modified value"]
pub fn alignment(mut self, alignment: Alignment) -> Title<'a> {
pub const fn alignment(mut self, alignment: Alignment) -> Title<'a> {
self.alignment = Some(alignment);
self
}
/// Set the title position.
#[must_use = "method moves the value of self and returns the modified value"]
pub fn position(mut self, position: Position) -> Title<'a> {
pub const fn position(mut self, position: Position) -> Title<'a> {
self.position = Some(position);
self
}

View file

@ -671,7 +671,7 @@ where
///
/// This is a fluent setter method which must be chained or used as it consumes self
#[must_use = "method moves the value of self and returns the modified value"]
pub fn x_bounds(mut self, bounds: [f64; 2]) -> Canvas<'a, F> {
pub const fn x_bounds(mut self, bounds: [f64; 2]) -> Canvas<'a, F> {
self.x_bounds = bounds;
self
}
@ -683,7 +683,7 @@ where
///
/// This is a fluent setter method which must be chained or used as it consumes self
#[must_use = "method moves the value of self and returns the modified value"]
pub fn y_bounds(mut self, bounds: [f64; 2]) -> Canvas<'a, F> {
pub const fn y_bounds(mut self, bounds: [f64; 2]) -> Canvas<'a, F> {
self.y_bounds = bounds;
self
}
@ -701,7 +701,7 @@ where
///
/// This is a fluent setter method which must be chained or used as it consumes self
#[must_use = "method moves the value of self and returns the modified value"]
pub fn background_color(mut self, color: Color) -> Canvas<'a, F> {
pub const fn background_color(mut self, color: Color) -> Canvas<'a, F> {
self.background_color = color;
self
}
@ -744,7 +744,7 @@ where
/// .marker(symbols::Marker::Block)
/// .paint(|ctx| {});
/// ```
pub fn marker(mut self, marker: symbols::Marker) -> Canvas<'a, F> {
pub const fn marker(mut self, marker: symbols::Marker) -> Canvas<'a, F> {
self.marker = marker;
self
}

View file

@ -70,7 +70,7 @@ impl<'a> Axis<'a> {
///
/// This is a fluent setter method which must be chained or used as it consumes self
#[must_use = "method moves the value of self and returns the modified value"]
pub fn bounds(mut self, bounds: [f64; 2]) -> Axis<'a> {
pub const fn bounds(mut self, bounds: [f64; 2]) -> Axis<'a> {
self.bounds = bounds;
self
}
@ -133,7 +133,7 @@ impl<'a> Axis<'a> {
///
/// On the X axis, this parameter only affects the first label.
#[must_use = "method moves the value of self and returns the modified value"]
pub fn labels_alignment(mut self, alignment: Alignment) -> Axis<'a> {
pub const fn labels_alignment(mut self, alignment: Alignment) -> Axis<'a> {
self.labels_alignment = alignment;
self
}
@ -341,7 +341,7 @@ impl<'a> Dataset<'a> {
///
/// This is a fluent setter method which must be chained or used as it consumes self
#[must_use = "method moves the value of self and returns the modified value"]
pub fn data(mut self, data: &'a [(f64, f64)]) -> Dataset<'a> {
pub const fn data(mut self, data: &'a [(f64, f64)]) -> Dataset<'a> {
self.data = data;
self
}
@ -356,7 +356,7 @@ impl<'a> Dataset<'a> {
///
/// This is a fluent setter method which must be chained or used as it consumes self
#[must_use = "method moves the value of self and returns the modified value"]
pub fn marker(mut self, marker: symbols::Marker) -> Dataset<'a> {
pub const fn marker(mut self, marker: symbols::Marker) -> Dataset<'a> {
self.marker = marker;
self
}
@ -369,7 +369,7 @@ impl<'a> Dataset<'a> {
///
/// This is a fluent setter method which must be chained or used as it consumes self
#[must_use = "method moves the value of self and returns the modified value"]
pub fn graph_type(mut self, graph_type: GraphType) -> Dataset<'a> {
pub const fn graph_type(mut self, graph_type: GraphType) -> Dataset<'a> {
self.graph_type = graph_type;
self
}
@ -648,7 +648,10 @@ impl<'a> Chart<'a> {
/// let chart = Chart::new(vec![]).hidden_legend_constraints(constraints);
/// ```
#[must_use = "method moves the value of self and returns the modified value"]
pub fn hidden_legend_constraints(mut self, constraints: (Constraint, Constraint)) -> Chart<'a> {
pub const fn hidden_legend_constraints(
mut self,
constraints: (Constraint, Constraint),
) -> Chart<'a> {
self.hidden_legend_constraints = constraints;
self
}
@ -683,7 +686,7 @@ impl<'a> Chart<'a> {
/// let chart = Chart::new(vec![]).legend_position(None);
/// ```
#[must_use = "method moves the value of self and returns the modified value"]
pub fn legend_position(mut self, position: Option<LegendPosition>) -> Chart<'a> {
pub const fn legend_position(mut self, position: Option<LegendPosition>) -> Chart<'a> {
self.legend_position = position;
self
}

View file

@ -147,7 +147,7 @@ impl<'a> Gauge<'a> {
/// [unicode block characters](https://en.wikipedia.org/wiki/Block_Elements).
/// This is useful to display a higher precision bar (8 extra fractional parts per cell).
#[must_use = "method moves the value of self and returns the modified value"]
pub fn use_unicode(mut self, unicode: bool) -> Gauge<'a> {
pub const fn use_unicode(mut self, unicode: bool) -> Gauge<'a> {
self.use_unicode = unicode;
self
}
@ -307,7 +307,7 @@ impl<'a> LineGauge<'a> {
/// [`NORMAL`](symbols::line::NORMAL), [`DOUBLE`](symbols::line::DOUBLE) and
/// [`THICK`](symbols::line::THICK).
#[must_use = "method moves the value of self and returns the modified value"]
pub fn line_set(mut self, set: symbols::line::Set) -> Self {
pub const fn line_set(mut self, set: symbols::line::Set) -> Self {
self.line_set = set;
self
}

View file

@ -62,7 +62,7 @@ impl ListState {
/// let state = ListState::default().with_offset(1);
/// ```
#[must_use = "method moves the value of self and returns the modified value"]
pub fn with_offset(mut self, offset: usize) -> Self {
pub const fn with_offset(mut self, offset: usize) -> Self {
self.offset = offset;
self
}
@ -78,7 +78,7 @@ impl ListState {
/// let state = ListState::default().with_selected(Some(1));
/// ```
#[must_use = "method moves the value of self and returns the modified value"]
pub fn with_selected(mut self, selected: Option<usize>) -> Self {
pub const fn with_selected(mut self, selected: Option<usize>) -> Self {
self.selected = selected;
self
}
@ -92,7 +92,7 @@ impl ListState {
/// let state = ListState::default();
/// assert_eq!(state.offset(), 0);
/// ```
pub fn offset(&self) -> usize {
pub const fn offset(&self) -> usize {
self.offset
}
@ -120,7 +120,7 @@ impl ListState {
/// let state = TableState::default();
/// assert_eq!(state.selected(), None);
/// ```
pub fn selected(&self) -> Option<usize> {
pub const fn selected(&self) -> Option<usize> {
self.selected
}
@ -594,7 +594,7 @@ impl<'a> List<'a> {
/// let list = List::new(items).highlight_symbol(">>");
/// ```
#[must_use = "method moves the value of self and returns the modified value"]
pub fn highlight_symbol(mut self, highlight_symbol: &'a str) -> List<'a> {
pub const fn highlight_symbol(mut self, highlight_symbol: &'a str) -> List<'a> {
self.highlight_symbol = Some(highlight_symbol);
self
}
@ -629,7 +629,7 @@ impl<'a> List<'a> {
///
/// This is a fluent setter method which must be chained or used as it consumes self
#[must_use = "method moves the value of self and returns the modified value"]
pub fn repeat_highlight_symbol(mut self, repeat: bool) -> List<'a> {
pub const fn repeat_highlight_symbol(mut self, repeat: bool) -> List<'a> {
self.repeat_highlight_symbol = repeat;
self
}
@ -660,7 +660,7 @@ impl<'a> List<'a> {
/// let list = List::new(items).highlight_spacing(HighlightSpacing::Always);
/// ```
#[must_use = "method moves the value of self and returns the modified value"]
pub fn highlight_spacing(mut self, value: HighlightSpacing) -> Self {
pub const fn highlight_spacing(mut self, value: HighlightSpacing) -> Self {
self.highlight_spacing = value;
self
}
@ -682,7 +682,7 @@ impl<'a> List<'a> {
/// let list = List::new(items).direction(ListDirection::BottomToTop);
/// ```
#[must_use = "method moves the value of self and returns the modified value"]
pub fn direction(mut self, direction: ListDirection) -> List<'a> {
pub const fn direction(mut self, direction: ListDirection) -> List<'a> {
self.direction = direction;
self
}
@ -701,7 +701,7 @@ impl<'a> List<'a> {
/// let list = List::new(items).scroll_padding(1);
/// ```
#[must_use = "method moves the value of self and returns the modified value"]
pub fn scroll_padding(mut self, padding: usize) -> List<'a> {
pub const fn scroll_padding(mut self, padding: usize) -> List<'a> {
self.scroll_padding = padding;
self
}

View file

@ -7,7 +7,7 @@ use crate::{
widgets::{reflow::*, Block},
};
fn get_line_offset(line_width: u16, text_area_width: u16, alignment: Alignment) -> u16 {
const fn get_line_offset(line_width: u16, text_area_width: u16, alignment: Alignment) -> u16 {
match alignment {
Alignment::Center => (text_area_width / 2).saturating_sub(line_width / 2),
Alignment::Right => text_area_width.saturating_sub(line_width),
@ -167,7 +167,7 @@ impl<'a> Paragraph<'a> {
/// let paragraph = Paragraph::new("Hello, world!").wrap(Wrap { trim: true });
/// ```
#[must_use = "method moves the value of self and returns the modified value"]
pub fn wrap(mut self, wrap: Wrap) -> Paragraph<'a> {
pub const fn wrap(mut self, wrap: Wrap) -> Paragraph<'a> {
self.wrap = Some(wrap);
self
}
@ -184,7 +184,7 @@ impl<'a> Paragraph<'a> {
/// For more information about future scrolling design and concerns, see [RFC: Design of
/// Scrollable Widgets](https://github.com/ratatui-org/ratatui/issues/174) on GitHub.
#[must_use = "method moves the value of self and returns the modified value"]
pub fn scroll(mut self, offset: (Vertical, Horizontal)) -> Paragraph<'a> {
pub const fn scroll(mut self, offset: (Vertical, Horizontal)) -> Paragraph<'a> {
self.scroll = offset;
self
}
@ -201,7 +201,7 @@ impl<'a> Paragraph<'a> {
/// let paragraph = Paragraph::new("Hello World").alignment(Alignment::Center);
/// ```
#[must_use = "method moves the value of self and returns the modified value"]
pub fn alignment(mut self, alignment: Alignment) -> Paragraph<'a> {
pub const fn alignment(mut self, alignment: Alignment) -> Paragraph<'a> {
self.alignment = alignment;
self
}
@ -217,7 +217,7 @@ impl<'a> Paragraph<'a> {
/// let paragraph = Paragraph::new("Hello World").left_aligned();
/// ```
#[must_use = "method moves the value of self and returns the modified value"]
pub fn left_aligned(self) -> Self {
pub const fn left_aligned(self) -> Self {
self.alignment(Alignment::Left)
}
@ -232,7 +232,7 @@ impl<'a> Paragraph<'a> {
/// let paragraph = Paragraph::new("Hello World").centered();
/// ```
#[must_use = "method moves the value of self and returns the modified value"]
pub fn centered(self) -> Self {
pub const fn centered(self) -> Self {
self.alignment(Alignment::Center)
}
@ -247,7 +247,7 @@ impl<'a> Paragraph<'a> {
/// let paragraph = Paragraph::new("Hello World").right_aligned();
/// ```
#[must_use = "method moves the value of self and returns the modified value"]
pub fn right_aligned(self) -> Self {
pub const fn right_aligned(self) -> Self {
self.alignment(Alignment::Right)
}

View file

@ -105,7 +105,7 @@ impl<'a> Sparkline<'a> {
/// # }
/// ```
#[must_use = "method moves the value of self and returns the modified value"]
pub fn data(mut self, data: &'a [u64]) -> Sparkline<'a> {
pub const fn data(mut self, data: &'a [u64]) -> Sparkline<'a> {
self.data = data;
self
}
@ -115,7 +115,7 @@ impl<'a> Sparkline<'a> {
/// Every bar will be scaled accordingly. If no max is given, this will be the max in the
/// dataset.
#[must_use = "method moves the value of self and returns the modified value"]
pub fn max(mut self, max: u64) -> Sparkline<'a> {
pub const fn max(mut self, max: u64) -> Sparkline<'a> {
self.max = Some(max);
self
}
@ -125,7 +125,7 @@ impl<'a> Sparkline<'a> {
/// Can be [`symbols::bar::THREE_LEVELS`], [`symbols::bar::NINE_LEVELS`] (default) or a custom
/// [`Set`](symbols::bar::Set).
#[must_use = "method moves the value of self and returns the modified value"]
pub fn bar_set(mut self, bar_set: symbols::bar::Set) -> Sparkline<'a> {
pub const fn bar_set(mut self, bar_set: symbols::bar::Set) -> Sparkline<'a> {
self.bar_set = bar_set;
self
}
@ -134,7 +134,7 @@ impl<'a> Sparkline<'a> {
///
/// [`RenderDirection::LeftToRight`] by default.
#[must_use = "method moves the value of self and returns the modified value"]
pub fn direction(mut self, direction: RenderDirection) -> Sparkline<'a> {
pub const fn direction(mut self, direction: RenderDirection) -> Sparkline<'a> {
self.direction = direction;
self
}

View file

@ -29,7 +29,7 @@ impl HighlightSpacing {
/// has_selection: true if a row is selected in the table
///
/// Returns true if a selection column should be displayed
pub(crate) fn should_add(&self, has_selection: bool) -> bool {
pub(crate) const fn should_add(&self, has_selection: bool) -> bool {
match self {
HighlightSpacing::Always => true,
HighlightSpacing::WhenSelected => has_selection,

View file

@ -145,7 +145,7 @@ impl<'a> Row<'a> {
/// let row = Row::new(cells).height(2);
/// ```
#[must_use = "method moves the value of self and returns the modified value"]
pub fn height(mut self, height: u16) -> Self {
pub const fn height(mut self, height: u16) -> Self {
self.height = height;
self
}
@ -164,7 +164,7 @@ impl<'a> Row<'a> {
/// let row = Row::default().top_margin(1);
/// ```
#[must_use = "method moves the value of self and returns the modified value"]
pub fn top_margin(mut self, margin: u16) -> Self {
pub const fn top_margin(mut self, margin: u16) -> Self {
self.top_margin = margin;
self
}
@ -183,7 +183,7 @@ impl<'a> Row<'a> {
/// let row = Row::default().bottom_margin(1);
/// ```
#[must_use = "method moves the value of self and returns the modified value"]
pub fn bottom_margin(mut self, margin: u16) -> Self {
pub const fn bottom_margin(mut self, margin: u16) -> Self {
self.bottom_margin = margin;
self
}
@ -224,7 +224,7 @@ impl<'a> Row<'a> {
// private methods for rendering
impl Row<'_> {
/// Returns the total height of the row.
pub(crate) fn height_with_margin(&self) -> u16 {
pub(crate) const fn height_with_margin(&self) -> u16 {
self.height
.saturating_add(self.top_margin)
.saturating_add(self.bottom_margin)

View file

@ -400,7 +400,7 @@ impl<'a> Table<'a> {
/// let table = Table::new(rows, widths).column_spacing(1);
/// ```
#[must_use = "method moves the value of self and returns the modified value"]
pub fn column_spacing(mut self, spacing: u16) -> Self {
pub const fn column_spacing(mut self, spacing: u16) -> Self {
self.column_spacing = spacing;
self
}
@ -530,7 +530,7 @@ impl<'a> Table<'a> {
/// let table = Table::new(rows, widths).highlight_spacing(HighlightSpacing::Always);
/// ```
#[must_use = "method moves the value of self and returns the modified value"]
pub fn highlight_spacing(mut self, value: HighlightSpacing) -> Self {
pub const fn highlight_spacing(mut self, value: HighlightSpacing) -> Self {
self.highlight_spacing = value;
self
}

View file

@ -75,7 +75,7 @@ impl TableState {
/// let state = TableState::new().with_offset(1);
/// ```
#[must_use = "method moves the value of self and returns the modified value"]
pub fn with_offset(mut self, offset: usize) -> Self {
pub const fn with_offset(mut self, offset: usize) -> Self {
self.offset = offset;
self
}
@ -108,7 +108,7 @@ impl TableState {
/// let state = TableState::new();
/// assert_eq!(state.offset(), 0);
/// ```
pub fn offset(&self) -> usize {
pub const fn offset(&self) -> usize {
self.offset
}
@ -136,7 +136,7 @@ impl TableState {
/// let state = TableState::new();
/// assert_eq!(state.selected(), None);
/// ```
pub fn selected(&self) -> Option<usize> {
pub const fn selected(&self) -> Option<usize> {
self.selected
}

View file

@ -113,7 +113,7 @@ impl<'a> Tabs<'a> {
/// The first tab has index 0 (this is also the default index).
/// The selected tab can have a different style with [`Tabs::highlight_style`].
#[must_use = "method moves the value of self and returns the modified value"]
pub fn select(mut self, selected: usize) -> Tabs<'a> {
pub const fn select(mut self, selected: usize) -> Tabs<'a> {
self.selected = selected;
self
}