Skip to content

Commit

Permalink
perf: clippy::missing_const_for_fn (#974)
Browse files Browse the repository at this point in the history
  • Loading branch information
EdJoPaTo authored and joshka committed Mar 4, 2024
1 parent 27680c0 commit 3834374
Show file tree
Hide file tree
Showing 25 changed files with 71 additions and 65 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
2 changes: 1 addition & 1 deletion src/backend/crossterm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/backend/termion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/backend/termwiz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion src/backend/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion src/buffer/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions src/terminal/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
}
2 changes: 1 addition & 1 deletion src/terminal/terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ where
}

/// Gets the backend
pub fn backend(&self) -> &B {
pub const fn backend(&self) -> &B {
&self.backend
}

Expand Down
2 changes: 1 addition & 1 deletion src/text/masked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
10 changes: 5 additions & 5 deletions src/widgets/barchart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion src/widgets/barchart/bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions src/widgets/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion src/widgets/block/padding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions src/widgets/block/title.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
8 changes: 4 additions & 4 deletions src/widgets/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
17 changes: 10 additions & 7 deletions src/widgets/chart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions src/widgets/gauge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
18 changes: 9 additions & 9 deletions src/widgets/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down

0 comments on commit 3834374

Please sign in to comment.