diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index aa168fe7..5fcf1e67 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -14,8 +14,10 @@ jobs: fail-fast: false matrix: os: [ubuntu-latest, macos-latest, windows-latest] + rust: [nightly, stable] runs-on: ${{ matrix.os }} + continue-on-error: ${{ matrix.rust == 'nightly' }} steps: - uses: actions/checkout@v2 @@ -23,7 +25,8 @@ jobs: - name: Install Rust uses: actions-rs/toolchain@v1 with: - toolchain: stable + toolchain: ${{ matrix.rust }} + default: true profile: minimal components: clippy diff --git a/asyncgit/src/sync/hunks.rs b/asyncgit/src/sync/hunks.rs index e0e86dad..99a72de3 100644 --- a/asyncgit/src/sync/hunks.rs +++ b/asyncgit/src/sync/hunks.rs @@ -111,7 +111,13 @@ pub fn unstage_hunk( let diff = get_diff_raw(&repo, &file_path, true, true)?; - assert_eq!(diff.deltas().len(), diff_count_positive); + if diff.deltas().len() != diff_count_positive { + return Err(Error::Generic(format!( + "hunk error: {}!={}", + diff.deltas().len(), + diff_count_positive + ))); + } let mut count = 0; { diff --git a/src/cmdbar.rs b/src/cmdbar.rs index 4496b00d..26ae7050 100644 --- a/src/cmdbar.rs +++ b/src/cmdbar.rs @@ -124,7 +124,7 @@ impl CommandBar { self.refresh_list(self.width); } - pub fn height(&self) -> u16 { + pub const fn height(&self) -> u16 { if self.expandable && self.expanded { self.lines } else { diff --git a/src/components/command.rs b/src/components/command.rs index 8c6a6e07..0f855321 100644 --- a/src/components/command.rs +++ b/src/components/command.rs @@ -63,24 +63,28 @@ impl CommandInfo { order: 0, } } + /// pub const fn order(self, order: i8) -> Self { let mut res = self; res.order = order; res } + /// pub const fn hidden(self) -> Self { let mut res = self; res.quick_bar = false; res } + /// pub fn print(&self, out: &mut String) { out.push_str(&self.text.name); } + /// - pub fn show_in_quickbar(&self) -> bool { + pub const fn show_in_quickbar(&self) -> bool { self.quick_bar && self.available } } diff --git a/src/components/commit.rs b/src/components/commit.rs index 72ce7cf3..3643a586 100644 --- a/src/components/commit.rs +++ b/src/components/commit.rs @@ -208,11 +208,10 @@ impl CommitComponent { return Ok(()); } - let res = if let Some(amend) = self.amend { - sync::amend(CWD, amend, &msg) - } else { - sync::commit(CWD, &msg) - }; + let res = + self.amend.map_or(sync::commit(CWD, &msg), |amend| { + sync::amend(CWD, amend, &msg) + }); if let Err(e) = res { log::error!("commit error: {}", &e); self.queue.borrow_mut().push_back( diff --git a/src/components/commit_details/details.rs b/src/components/commit_details/details.rs index 8d15bebd..0ef5c738 100644 --- a/src/components/commit_details/details.rs +++ b/src/components/commit_details/details.rs @@ -69,11 +69,8 @@ impl DetailsComponent { ) -> Result<()> { self.tags.clear(); - self.data = if let Some(id) = id { - sync::get_commit_details(CWD, id).ok() - } else { - None - }; + self.data = + id.and_then(|id| sync::get_commit_details(CWD, id).ok()); self.scroll_top.set(0); diff --git a/src/components/commitlist.rs b/src/components/commitlist.rs index 66cdf663..8608172c 100644 --- a/src/components/commitlist.rs +++ b/src/components/commitlist.rs @@ -252,13 +252,11 @@ impl CommitList { .take(height) .enumerate() { - let tags = if let Some(tags) = - self.tags.as_ref().and_then(|t| t.get(&e.id)) - { - Some(tags.join(" ")) - } else { - None - }; + let tags = self + .tags + .as_ref() + .and_then(|t| t.get(&e.id)) + .map(|tags| tags.join(" ")); Self::add_entry( e, diff --git a/src/components/diff.rs b/src/components/diff.rs index f3387010..e2ea94ec 100644 --- a/src/components/diff.rs +++ b/src/components/diff.rs @@ -38,13 +38,13 @@ enum Selection { } impl Selection { - fn get_start(&self) -> usize { + const fn get_start(&self) -> usize { match self { Self::Single(start) | Self::Multiple(start, _) => *start, } } - fn get_end(&self) -> usize { + const fn get_end(&self) -> usize { match self { Self::Single(end) | Self::Multiple(_, end) => *end, } @@ -447,7 +447,7 @@ impl DiffComponent { )); } - fn hunk_visible( + const fn hunk_visible( hunk_min: usize, hunk_max: usize, min: usize, diff --git a/src/components/filetree.rs b/src/components/filetree.rs index 56a77e5e..8305d0c1 100644 --- a/src/components/filetree.rs +++ b/src/components/filetree.rs @@ -113,14 +113,12 @@ impl FileTreeComponent { /// pub fn is_file_seleted(&self) -> bool { - if let Some(item) = self.tree.selected_item() { + self.tree.selected_item().map_or(false, |item| { match item.kind { FileTreeItemKind::File(_) => true, FileTreeItemKind::Path(..) => false, } - } else { - false - } + }) } fn move_selection(&mut self, dir: MoveSelection) -> bool { @@ -207,7 +205,7 @@ impl FileTreeComponent { } } - fn item_status_char(item_type: StatusItemType) -> char { + const fn item_status_char(item_type: StatusItemType) -> char { match item_type { StatusItemType::Modified => 'M', StatusItemType::New => '+', diff --git a/src/components/textinput.rs b/src/components/textinput.rs index 651a3f7e..36a2ad42 100644 --- a/src/components/textinput.rs +++ b/src/components/textinput.rs @@ -122,14 +122,11 @@ impl TextInputComponent { )); } - let cursor_str = if let Some(pos) = self.next_char_position() - { - &self.msg[self.cursor_position..pos] - } else { + let cursor_str = self + .next_char_position() // if the cursor is at the end of the msg // a whitespace is used to underline - " " - }; + .map_or(" ", |pos| &self.msg[self.cursor_position..pos]); if cursor_str == "\n" { txt.push(Text::styled( diff --git a/src/components/utils/statustree.rs b/src/components/utils/statustree.rs index 1c930385..a5c6049a 100644 --- a/src/components/utils/statustree.rs +++ b/src/components/utils/statustree.rs @@ -44,17 +44,16 @@ impl StatusTree { let last_selection_index = self.selection.unwrap_or(0); self.tree = FileTreeItems::new(list, &last_collapsed)?; - self.selection = - if let Some(ref last_selection) = last_selection { + self.selection = last_selection.as_ref().map_or( + self.tree.items().first().map(|_| 0), + |last_selection| { self.find_last_selection( last_selection, last_selection_index, ) .or_else(|| self.tree.items().first().map(|_| 0)) - } else { - // simply select first - self.tree.items().first().map(|_| 0) - }; + }, + ); self.update_visibility(None, 0, true); @@ -80,7 +79,7 @@ impl StatusTree { /// pub fn move_selection(&mut self, dir: MoveSelection) -> bool { - if let Some(selection) = self.selection { + self.selection.map_or(false, |selection| { let selection_change = match dir { MoveSelection::Up => { self.selection_updown(selection, true) @@ -102,9 +101,7 @@ impl StatusTree { self.selection = Some(selection_change.new_index); changed_index || selection_change.changes - } else { - false - } + }) } /// diff --git a/src/main.rs b/src/main.rs index 911ed11f..116a9f4b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,7 @@ #![deny(clippy::panic)] #![allow(clippy::module_name_repetitions)] #![allow(clippy::multiple_crate_versions)] +#![warn(clippy::missing_const_for_fn)] mod app; mod cmdbar; diff --git a/src/tabs/revlog.rs b/src/tabs/revlog.rs index 633e157c..90f85135 100644 --- a/src/tabs/revlog.rs +++ b/src/tabs/revlog.rs @@ -158,15 +158,7 @@ impl Revlog { let tags = self.list.tags(); commit.and_then(|commit| { - if let Some(tags) = tags { - if let Some(tags) = tags.get(&commit) { - Some(tags.clone()) - } else { - None - } - } else { - None - } + tags.and_then(|tags| tags.get(&commit).cloned()) }) } } @@ -213,28 +205,32 @@ impl Component for Revlog { self.update()?; return Ok(true); } else if k == self.key_config.log_tag_commit { - return if let Some(id) = self.selected_commit() { - self.queue - .borrow_mut() - .push_back(InternalEvent::TagCommit(id)); - Ok(true) - } else { - Ok(false) - }; + return self.selected_commit().map_or( + Ok(false), + |id| { + self.queue.borrow_mut().push_back( + InternalEvent::TagCommit(id), + ); + Ok(true) + }, + ); } else if k == self.key_config.focus_right && self.commit_details.is_visible() { - return if let Some(id) = self.selected_commit() { - self.queue.borrow_mut().push_back( - InternalEvent::InspectCommit( - id, - self.selected_commit_tags(&Some(id)), - ), - ); - Ok(true) - } else { - Ok(false) - }; + return self.selected_commit().map_or( + Ok(false), + |id| { + self.queue.borrow_mut().push_back( + InternalEvent::InspectCommit( + id, + self.selected_commit_tags(&Some( + id, + )), + ), + ); + Ok(true) + }, + ); } else { } } diff --git a/src/ui/style.rs b/src/ui/style.rs index dc632d87..3a33c5ed 100644 --- a/src/ui/style.rs +++ b/src/ui/style.rs @@ -116,7 +116,11 @@ impl Theme { self.apply_select(style, selected) } - fn apply_select(&self, style: Style, selected: bool) -> Style { + const fn apply_select( + &self, + style: Style, + selected: bool, + ) -> Style { if selected { style.bg(self.selection_bg) } else {