Skip to content

Commit

Permalink
chore: updating clippy lints in ad (not tackling missing docs yet)
Browse files Browse the repository at this point in the history
  • Loading branch information
sminez committed Sep 10, 2024
1 parent cd5ae5a commit 83ae1f2
Show file tree
Hide file tree
Showing 18 changed files with 93 additions and 61 deletions.
16 changes: 10 additions & 6 deletions src/buffer/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ fn count_chars(bytes: &[u8]) -> usize {
let mut cur = 0;

while cur < bytes.len() {
// SAFETY: we know we are in bounds and that we contain valid utf-8 data
let ch = unsafe { decode_char_at(cur, bytes) };
cur += ch.len_utf8();
n_chars += 1;
Expand Down Expand Up @@ -201,7 +202,7 @@ impl GapBuffer {
v
}

pub fn iter_lines(&self) -> impl Iterator<Item = Slice> {
pub fn iter_lines(&self) -> impl Iterator<Item = Slice<'_>> {
let mut line_idx = 0;

std::iter::from_fn(move || {
Expand Down Expand Up @@ -254,7 +255,7 @@ impl GapBuffer {
pub fn char(&self, char_idx: usize) -> char {
let byte_idx = self.char_to_raw_byte(char_idx);

// SAFTEY: we know that we have valid utf8 data internally
// SAFETY: we know that we have valid utf8 data internally
unsafe { decode_char_at(byte_idx, &self.data) }
}

Expand All @@ -269,12 +270,12 @@ impl GapBuffer {

#[inline]
fn char_len(&self, byte_idx: usize) -> usize {
// SAFTEY: we know that we have valid utf8 data internally
// SAFETY: we know that we have valid utf8 data internally
unsafe { decode_char_at(byte_idx, &self.data) }.len_utf8()
}

#[inline]
pub fn line(&self, line_idx: usize) -> Slice {
pub fn line(&self, line_idx: usize) -> Slice<'_> {
if line_idx >= self.len_lines() {
panic!(
"line index was {line_idx} but buffer has {} lines",
Expand Down Expand Up @@ -321,7 +322,7 @@ impl GapBuffer {
}

/// An exclusive range of characters from the buffer
pub fn slice(&self, char_from: usize, char_to: usize) -> Slice {
pub fn slice(&self, char_from: usize, char_to: usize) -> Slice<'_> {
let from = self.char_to_raw_byte(char_from);
let to = self.char_to_raw_byte(char_to);

Expand Down Expand Up @@ -703,7 +704,7 @@ impl<'a> Slice<'a> {
}

pub fn as_strs(&self) -> (&str, &str) {
// SAFTEY: we know that we have valid utf8 data internally
// SAFETY: we know that we have valid utf8 data internally
unsafe {
(
std::str::from_utf8_unchecked(self.left),
Expand Down Expand Up @@ -771,6 +772,7 @@ impl<'a> Iterator for Chars<'a> {
}

let (cur, data) = self.s.cur_and_data(self.cur);
// SAFETY: we know we are in bounds and that we contain valid utf-8 data
let ch = unsafe { decode_char_at(cur, data) };
let len = ch.len_utf8();
self.cur += len;
Expand Down Expand Up @@ -798,13 +800,15 @@ impl<'a> Iterator for IdxChars<'a> {

if self.rev {
let (cur, data) = self.s.cur_and_data(self.cur - 1);
// SAFETY: we know we are in bounds and that we contain valid utf-8 data
let ch = unsafe { decode_char_ending_at(cur, data) };
let len = ch.len_utf8();
self.idx -= 1;
self.cur -= len;
Some((self.idx, ch))
} else {
let (cur, data) = self.s.cur_and_data(self.cur);
// SAFETY: we know we are in bounds and that we contain valid utf-8 data
let ch = unsafe { decode_char_at(cur, data) };
let len = ch.len_utf8();
let res = Some((self.idx, ch));
Expand Down
2 changes: 1 addition & 1 deletion src/buffer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ impl Buffer {
cx
}

pub fn line(&self, y: usize) -> Option<Slice> {
pub fn line(&self, y: usize) -> Option<Slice<'_>> {
if y >= self.len_lines() {
None
} else {
Expand Down
1 change: 1 addition & 0 deletions src/editor/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::{
thread::{spawn, JoinHandle},
};

#[derive(Debug)]
pub enum InputEvent {
Message(Message),
KeyPress(Key),
Expand Down
4 changes: 3 additions & 1 deletion src/editor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub enum EditorMode {
Headless,
}

#[derive(Debug)]
pub struct Editor {
screen_rows: usize,
screen_cols: usize,
Expand Down Expand Up @@ -164,7 +165,8 @@ impl Editor {

enable_mouse_support(&mut self.stdout);
enable_alternate_screen(&mut self.stdout);
register_signal_handler();
// SAFETY: we only register our signal handler once
unsafe { register_signal_handler() };
self.update_window_size();
Input::new(tx).run_threaded();
}
Expand Down
8 changes: 4 additions & 4 deletions src/exec/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl Addr {
}

/// Attempt to parse a valid dot expression from a character stream
pub fn parse(it: &mut Peekable<Chars>) -> Result<Self, ParseError> {
pub fn parse(it: &mut Peekable<Chars<'_>>) -> Result<Self, ParseError> {
let start = match SimpleAddr::parse(it) {
Ok(exp) => Some(exp),
// If the following char is a ',' we substitute BOF for a missing start
Expand Down Expand Up @@ -91,7 +91,7 @@ pub struct SimpleAddr {
}

impl SimpleAddr {
fn parse(it: &mut Peekable<Chars>) -> Result<Self, ParseError> {
fn parse(it: &mut Peekable<Chars<'_>>) -> Result<Self, ParseError> {
let base = AddrBase::parse(it)?;
let mut suffix = String::with_capacity(2);

Expand Down Expand Up @@ -159,7 +159,7 @@ enum Dir {
}

impl AddrBase {
pub(crate) fn parse(it: &mut Peekable<Chars>) -> Result<Self, ParseError> {
pub(crate) fn parse(it: &mut Peekable<Chars<'_>>) -> Result<Self, ParseError> {
let dir = match it.peek() {
Some('-') => {
it.next();
Expand Down Expand Up @@ -242,7 +242,7 @@ impl AddrBase {
}
}

fn parse_delimited_regex(it: &mut Peekable<Chars>, dir: Dir) -> Result<AddrBase, ParseError> {
fn parse_delimited_regex(it: &mut Peekable<Chars<'_>>, dir: Dir) -> Result<AddrBase, ParseError> {
let mut s = String::new();
let mut prev = '/';

Expand Down
3 changes: 3 additions & 0 deletions src/exec/cached_stdin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ const LINE_BUF_LEN: usize = 100;

/// A wrapper around stdin that buffers and caches input so that it can be manipulated
/// like a Buffer
#[derive(Debug)]
pub struct CachedStdin {
inner: RefCell<CachedStdinInner>,
buf: RefCell<String>,
gb: RefCell<GapBuffer>,
}

#[derive(Debug)]
struct CachedStdinInner {
stdin: Stdin,
closed: bool,
Expand Down Expand Up @@ -132,6 +134,7 @@ impl Edit for CachedStdin {
}
}

#[derive(Debug)]
pub struct CachedStdinIter<'a> {
pub(super) inner: &'a CachedStdin,
pub(super) from: usize,
Expand Down
13 changes: 7 additions & 6 deletions src/exec/char_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,27 @@ pub trait IterBoundedChars {
}

impl IterBoundedChars for GapBuffer {
fn iter_between(&self, from: usize, to: usize) -> CharIter {
fn iter_between(&self, from: usize, to: usize) -> CharIter<'_> {
CharIter::Slice(self.slice(from, to).indexed_chars(from, false))
}

fn rev_iter_between(&self, from: usize, to: usize) -> CharIter {
fn rev_iter_between(&self, from: usize, to: usize) -> CharIter<'_> {
CharIter::Slice(self.slice(to, from).indexed_chars(to, true))
}
}

impl IterBoundedChars for Buffer {
fn iter_between(&self, from: usize, to: usize) -> CharIter {
fn iter_between(&self, from: usize, to: usize) -> CharIter<'_> {
CharIter::Slice(self.txt.slice(from, to).indexed_chars(from, false))
}

fn rev_iter_between(&self, from: usize, to: usize) -> CharIter {
fn rev_iter_between(&self, from: usize, to: usize) -> CharIter<'_> {
CharIter::Slice(self.txt.slice(to, from).indexed_chars(to, true))
}
}

/// Supported iterator types that can be returned by an InterBoundedChars
#[derive(Debug)]
pub enum CharIter<'a> {
Slice(IdxChars<'a>),
StdIn(CachedStdinIter<'a>),
Expand All @@ -60,7 +61,7 @@ impl<'a> Iterator for CharIter<'a> {
}

impl IterBoundedChars for CachedStdin {
fn iter_between(&self, from: usize, to: usize) -> CharIter {
fn iter_between(&self, from: usize, to: usize) -> CharIter<'_> {
CharIter::StdIn(CachedStdinIter {
inner: self,
from,
Expand All @@ -69,7 +70,7 @@ impl IterBoundedChars for CachedStdin {
}

/// This will always return None
fn rev_iter_between(&self, from: usize, to: usize) -> CharIter {
fn rev_iter_between(&self, from: usize, to: usize) -> CharIter<'_> {
CharIter::StdIn(CachedStdinIter {
inner: self,
from,
Expand Down
16 changes: 10 additions & 6 deletions src/exec/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub(super) enum ParseOutput {
}

impl Expr {
pub(super) fn try_parse(it: &mut Peekable<Chars>) -> Result<ParseOutput, Error> {
pub(super) fn try_parse(it: &mut Peekable<Chars<'_>>) -> Result<ParseOutput, Error> {
use Expr::*;
use ParseOutput::*;

Expand Down Expand Up @@ -70,17 +70,21 @@ impl Expr {
}
}

fn parse_delimited_regex(it: &mut Peekable<Chars>, kind: &'static str) -> Result<Regex, Error> {
fn parse_delimited_regex(it: &mut Peekable<Chars<'_>>, kind: &'static str) -> Result<Regex, Error> {
let s = parse_delimited_str(it, kind)?;
Ok(Regex::compile(&s)?)
}

fn parse_delimited_str(it: &mut Peekable<Chars>, kind: &'static str) -> Result<String, Error> {
fn parse_delimited_str(it: &mut Peekable<Chars<'_>>, kind: &'static str) -> Result<String, Error> {
let delim = it.next().ok_or(Error::MissingDelimiter(kind))?;
read_until(delim, it, kind)
}

fn read_until(delim: char, it: &mut Peekable<Chars>, kind: &'static str) -> Result<String, Error> {
fn read_until(
delim: char,
it: &mut Peekable<Chars<'_>>,
kind: &'static str,
) -> Result<String, Error> {
let mut s = String::new();
let mut prev = delim;

Expand All @@ -95,7 +99,7 @@ fn read_until(delim: char, it: &mut Peekable<Chars>, kind: &'static str) -> Resu
Err(Error::UnclosedDelimiter(kind, delim))
}

fn parse_sub(it: &mut Peekable<Chars>) -> Result<ParseOutput, Error> {
fn parse_sub(it: &mut Peekable<Chars<'_>>) -> Result<ParseOutput, Error> {
let delim = it.next().ok_or(Error::MissingDelimiter("s"))?;
let re = Regex::compile(&read_until(delim, it, "s")?)?;
let s = read_until(delim, it, "s")?;
Expand All @@ -107,7 +111,7 @@ fn parse_sub(it: &mut Peekable<Chars>) -> Result<ParseOutput, Error> {
}
}

fn parse_group(it: &mut Peekable<Chars>) -> Result<Vec<Vec<Expr>>, Error> {
fn parse_group(it: &mut Peekable<Chars<'_>>) -> Result<Vec<Vec<Expr>>, Error> {
let mut group = Vec::new();
let mut branch = Vec::new();
loop {
Expand Down
2 changes: 1 addition & 1 deletion src/exec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ impl Program {
}
}

fn consume_whitespace(it: &mut Peekable<Chars>) {
fn consume_whitespace(it: &mut Peekable<Chars<'_>>) {
loop {
match it.peek() {
Some(ch) if ch.is_whitespace() => {
Expand Down
12 changes: 6 additions & 6 deletions src/ftype/lex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,27 +329,27 @@ mod tests {
use crate::ftype::RUST_SPEC;
use simple_test_case::test_case;

fn t_def(s: &str) -> Token {
fn t_def(s: &str) -> Token<'_> {
Token { ty: Default, s }
}

fn t_com(s: &str) -> Token {
fn t_com(s: &str) -> Token<'_> {
Token { ty: Comment, s }
}

fn t_dot(s: &str) -> Token {
fn t_dot(s: &str) -> Token<'_> {
Token { ty: Dot, s }
}

fn t_dfn(s: &str) -> Token {
fn t_dfn(s: &str) -> Token<'_> {
Token { ty: Definition, s }
}

fn t_cfl(s: &str) -> Token {
fn t_cfl(s: &str) -> Token<'_> {
Token { ty: ControlFlow, s }
}

fn t_str(s: &str) -> Token {
fn t_str(s: &str) -> Token<'_> {
Token { ty: String, s }
}

Expand Down
12 changes: 12 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
//! ad :: the adaptable editor
#![warn(
clippy::complexity,
clippy::correctness,
clippy::style,
future_incompatible,
missing_debug_implementations,
// missing_docs,
rust_2018_idioms,
rustdoc::all,
clippy::undocumented_unsafe_blocks
)]

use libc::termios as Termios;
use std::{
io::Stdout,
Expand Down
1 change: 1 addition & 0 deletions src/mode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub(crate) fn modes() -> Vec<Mode> {
vec![normal::normal_mode(), insert::insert_mode()]
}

#[derive(Debug)]
pub struct Mode {
pub(crate) name: String,
pub(crate) cur_shape: CurShape,
Expand Down
Loading

0 comments on commit 83ae1f2

Please sign in to comment.