fix more issues uncovered by the latest clippy

This commit is contained in:
Damien Elmes 2021-03-27 20:25:34 +10:00
parent dc81a7fed0
commit 1055acb9f2
19 changed files with 59 additions and 74 deletions

View file

@ -51,9 +51,9 @@ impl From<pb::CardId> for CardId {
}
}
impl Into<Vec<CardId>> for pb::CardIDs {
fn into(self) -> Vec<CardId> {
self.cids.into_iter().map(CardId).collect()
impl From<pb::CardIDs> for Vec<CardId> {
fn from(c: pb::CardIDs) -> Self {
c.cids.into_iter().map(CardId).collect()
}
}

View file

@ -1,6 +1,10 @@
// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
// infallible backend methods still return a result
#![allow(clippy::unnecessary_wraps)]
#![allow(clippy::unknown_clippy_lints)]
mod adding;
mod card;
mod cardrendering;

View file

@ -16,7 +16,7 @@ impl StatsService for Backend {
}
fn get_graph_preferences(&self, _input: pb::Empty) -> Result<pb::GraphPreferences> {
self.with_col(|col| col.get_graph_preferences())
self.with_col(|col| Ok(col.get_graph_preferences()))
}
fn set_graph_preferences(&self, input: pb::GraphPreferences) -> Result<pb::Empty> {

View file

@ -103,7 +103,7 @@ impl Collection {
match res {
Ok(output) => {
let changes = if op.is_some() {
let changes = self.op_changes()?;
let changes = self.op_changes();
self.maybe_clear_study_queues_after_op(changes);
self.maybe_coalesce_note_undo_entry(changes);
changes

View file

@ -192,7 +192,7 @@ delete from media where fname=?"
.query_row(
"select count(*) from media where csum is not null",
NO_PARAMS,
|row| Ok(row.get(0)?),
|row| row.get(0),
)
.map_err(Into::into)
}

View file

@ -122,7 +122,11 @@ pub(crate) fn normalize_nfc_filename(mut fname: Cow<str>) -> Cow<str> {
/// On Apple devices, the filename may be stored on disk in NFD encoding,
/// but can be accessed as NFC. On these devices, if the filename
/// is otherwise valid, the filename is returned as NFC.
///
/// First two lints can be removed after updating to 1.51
#[allow(clippy::unknown_clippy_lints)]
#[allow(clippy::collapsible_if)]
#[allow(clippy::collapsible_else_if)]
pub(super) fn filename_if_normalized(fname: &str) -> Option<Cow<str>> {
if cfg!(target_vendor = "apple") {
if !is_nfc(fname) {

View file

@ -334,9 +334,8 @@ impl Collection {
fn default_deck_conf(&mut self) -> Result<(DeckId, DeckConfId)> {
// currently hard-coded to 1, we could create this as needed in the future
Ok(self
.deck_conf_if_normal(DeckId(1))?
.ok_or_else(|| AnkiError::invalid_input("invalid default deck"))?)
self.deck_conf_if_normal(DeckId(1))?
.ok_or_else(|| AnkiError::invalid_input("invalid default deck"))
}
/// If deck exists and and is a normal deck, return its ID and config

View file

@ -14,24 +14,19 @@ impl CardStateUpdater {
&mut self,
current: CardState,
next: NewState,
) -> Result<Option<RevlogEntryPartial>> {
) -> Option<RevlogEntryPartial> {
self.card.ctype = CardType::New;
self.card.queue = CardQueue::New;
self.card.due = next.position as i32;
Ok(RevlogEntryPartial::maybe_new(
current,
next.into(),
0.0,
self.secs_until_rollover(),
))
RevlogEntryPartial::maybe_new(current, next.into(), 0.0, self.secs_until_rollover())
}
pub(super) fn apply_learning_state(
&mut self,
current: CardState,
next: LearnState,
) -> Result<Option<RevlogEntryPartial>> {
) -> Option<RevlogEntryPartial> {
self.card.remaining_steps = next.remaining_steps;
self.card.ctype = CardType::Learn;
@ -49,11 +44,6 @@ impl CardStateUpdater {
}
}
Ok(RevlogEntryPartial::maybe_new(
current,
next.into(),
0.0,
self.secs_until_rollover(),
))
RevlogEntryPartial::maybe_new(current, next.into(), 0.0, self.secs_until_rollover())
}
}

View file

@ -133,7 +133,7 @@ impl CardStateUpdater {
}
}
}
}?;
};
Ok(revlog)
}
@ -142,7 +142,7 @@ impl CardStateUpdater {
&mut self,
current: CardState,
next: NormalState,
) -> Result<Option<RevlogEntryPartial>> {
) -> Option<RevlogEntryPartial> {
self.card.reps += 1;
self.card.original_due = 0;
@ -151,13 +151,13 @@ impl CardStateUpdater {
NormalState::Learning(next) => self.apply_learning_state(current, next),
NormalState::Review(next) => self.apply_review_state(current, next),
NormalState::Relearning(next) => self.apply_relearning_state(current, next),
}?;
};
if next.leeched() && self.config.inner.leech_action() == LeechAction::Suspend {
self.card.queue = CardQueue::Suspended;
}
Ok(revlog)
revlog
}
fn ensure_filtered(&self) -> Result<()> {

View file

@ -4,7 +4,6 @@
use crate::{
card::CardQueue,
config::SchedulerVersion,
prelude::*,
scheduler::states::{CardState, IntervalKind, PreviewState},
};
@ -17,11 +16,11 @@ impl CardStateUpdater {
&mut self,
current: CardState,
next: PreviewState,
) -> Result<Option<RevlogEntryPartial>> {
) -> Option<RevlogEntryPartial> {
if next.finished {
self.card
.remove_from_filtered_deck_restoring_queue(SchedulerVersion::V2);
return Ok(None);
return None;
}
self.card.queue = CardQueue::PreviewRepeat;
@ -36,18 +35,14 @@ impl CardStateUpdater {
}
}
Ok(RevlogEntryPartial::maybe_new(
current,
next.into(),
0.0,
self.secs_until_rollover(),
))
RevlogEntryPartial::maybe_new(current, next.into(), 0.0, self.secs_until_rollover())
}
}
#[cfg(test)]
mod test {
use crate::collection::open_test_collection;
use crate::prelude::*;
use super::*;
use crate::{

View file

@ -14,7 +14,7 @@ impl CardStateUpdater {
&mut self,
current: CardState,
next: RelearnState,
) -> Result<Option<RevlogEntryPartial>> {
) -> Option<RevlogEntryPartial> {
self.card.interval = next.review.scheduled_days;
self.card.remaining_steps = next.learning.remaining_steps;
self.card.ctype = CardType::Relearn;
@ -34,11 +34,11 @@ impl CardStateUpdater {
}
}
Ok(RevlogEntryPartial::maybe_new(
RevlogEntryPartial::maybe_new(
current,
next.into(),
next.review.ease_factor,
self.secs_until_rollover(),
))
)
}
}

View file

@ -3,7 +3,6 @@
use crate::{
card::{CardQueue, CardType},
prelude::*,
scheduler::states::{CardState, ReviewState},
};
@ -14,7 +13,7 @@ impl CardStateUpdater {
&mut self,
current: CardState,
next: ReviewState,
) -> Result<Option<RevlogEntryPartial>> {
) -> Option<RevlogEntryPartial> {
self.card.queue = CardQueue::Review;
self.card.ctype = CardType::Review;
self.card.interval = next.scheduled_days;
@ -22,11 +21,11 @@ impl CardStateUpdater {
self.card.ease_factor = (next.ease_factor * 1000.0).round() as u16;
self.card.lapses = next.lapses;
Ok(RevlogEntryPartial::maybe_new(
RevlogEntryPartial::maybe_new(
current,
next.into(),
next.ease_factor,
self.secs_until_rollover(),
))
)
}
}

View file

@ -140,11 +140,8 @@ impl Collection {
Ok(position)
}
fn get_next_filtered_deck_name(&self) -> Result<String> {
Ok(format!(
"Filtered Deck {}",
TimestampSecs::now().time_string()
))
fn get_next_filtered_deck_name(&self) -> String {
format!("Filtered Deck {}", TimestampSecs::now().time_string())
}
fn add_or_update_filtered_deck_inner(
@ -203,7 +200,7 @@ impl Collection {
fn new_filtered_deck_for_adding(&mut self) -> Result<Deck> {
let mut deck = Deck {
name: self.get_next_filtered_deck_name()?,
name: self.get_next_filtered_deck_name(),
..Deck::new_filtered()
};
if let Some(current) = self.get_deck(self.get_current_deck_id())? {

View file

@ -83,7 +83,7 @@ impl Collection {
SortMode::Builtin { kind, reverse } => {
prepare_sort(self, kind)?;
sql.push_str(" order by ");
write_order(sql, kind, reverse)?;
write_order(sql, kind, reverse);
}
SortMode::Custom(order_clause) => {
sql.push_str(" order by ");
@ -135,7 +135,7 @@ impl Collection {
}
/// Add the order clause to the sql.
fn write_order(sql: &mut String, kind: SortKind, reverse: bool) -> Result<()> {
fn write_order(sql: &mut String, kind: SortKind, reverse: bool) {
let tmp_str;
let order = match kind {
SortKind::NoteCreation => "n.id asc, c.ord asc",
@ -160,7 +160,7 @@ fn write_order(sql: &mut String, kind: SortKind, reverse: bool) -> Result<()> {
),
};
if order.is_empty() {
return Ok(());
return;
}
if reverse {
sql.push_str(
@ -172,7 +172,6 @@ fn write_order(sql: &mut String, kind: SortKind, reverse: bool) -> Result<()> {
} else {
sql.push_str(order);
}
Ok(())
}
fn needs_aux_sort_table(kind: SortKind) -> bool {

View file

@ -134,9 +134,9 @@ impl SqlWriter<'_> {
SearchNode::AddedInDays(days) => self.write_added(*days)?,
SearchNode::EditedInDays(days) => self.write_edited(*days)?,
SearchNode::CardTemplate(template) => match template {
TemplateKind::Ordinal(_) => self.write_template(template)?,
TemplateKind::Ordinal(_) => self.write_template(template),
TemplateKind::Name(name) => {
self.write_template(&TemplateKind::Name(norm(name).into()))?
self.write_template(&TemplateKind::Name(norm(name).into()))
}
},
SearchNode::Deck(deck) => self.write_deck(&norm(deck))?,
@ -146,10 +146,10 @@ impl SqlWriter<'_> {
SearchNode::DeckId(did) => {
write!(self.sql, "c.did = {}", did).unwrap();
}
SearchNode::NoteType(notetype) => self.write_note_type(&norm(notetype))?,
SearchNode::NoteType(notetype) => self.write_note_type(&norm(notetype)),
SearchNode::Rated { days, ease } => self.write_rated(">", -i64::from(*days), ease)?,
SearchNode::Tag(tag) => self.write_tag(&norm(tag))?,
SearchNode::Tag(tag) => self.write_tag(&norm(tag)),
SearchNode::State(state) => self.write_state(state)?,
SearchNode::Flag(flag) => {
write!(self.sql, "(c.flags & 7) == {}", flag).unwrap();
@ -192,7 +192,7 @@ impl SqlWriter<'_> {
.unwrap();
}
fn write_tag(&mut self, text: &str) -> Result<()> {
fn write_tag(&mut self, text: &str) {
if text.contains(' ') {
write!(self.sql, "false").unwrap();
} else {
@ -210,8 +210,6 @@ impl SqlWriter<'_> {
}
}
}
Ok(())
}
fn write_rated(&mut self, op: &str, days: i64, ease: &RatingKind) -> Result<()> {
@ -373,7 +371,7 @@ impl SqlWriter<'_> {
Ok(())
}
fn write_template(&mut self, template: &TemplateKind) -> Result<()> {
fn write_template(&mut self, template: &TemplateKind) {
match template {
TemplateKind::Ordinal(n) => {
write!(self.sql, "c.ord = {}", n).unwrap();
@ -393,10 +391,9 @@ impl SqlWriter<'_> {
}
}
};
Ok(())
}
fn write_note_type(&mut self, nt_name: &str) -> Result<()> {
fn write_note_type(&mut self, nt_name: &str) {
if is_glob(nt_name) {
let re = format!("(?i){}", to_re(nt_name));
self.sql
@ -407,7 +404,6 @@ impl SqlWriter<'_> {
.push_str("n.mid in (select id from notetypes where name = ?)");
self.args.push(to_text(nt_name).into());
}
Ok(())
}
fn write_single_field(&mut self, field_name: &str, val: &str, is_re: bool) -> Result<()> {

View file

@ -58,7 +58,7 @@ struct RevlogText {
impl Collection {
pub fn card_stats(&mut self, cid: CardId) -> Result<String> {
let stats = self.gather_card_stats(cid)?;
self.card_stats_to_string(stats)
Ok(self.card_stats_to_string(stats))
}
fn gather_card_stats(&mut self, cid: CardId) -> Result<CardStats> {
@ -126,7 +126,7 @@ impl Collection {
})
}
fn card_stats_to_string(&mut self, cs: CardStats) -> Result<String> {
fn card_stats_to_string(&mut self, cs: CardStats) -> String {
let tr = &self.tr;
let mut stats = vec![(tr.card_stats_added().into(), cs.added.date_string())];
@ -194,13 +194,13 @@ impl Collection {
taken_secs: tr.card_stats_review_log_time_taken().into(),
};
Ok(CardStatsTemplate {
CardStatsTemplate {
stats,
revlog,
revlog_titles,
}
.render()
.unwrap())
.unwrap()
}
}

View file

@ -51,13 +51,13 @@ impl Collection {
})
}
pub(crate) fn get_graph_preferences(&self) -> Result<pb::GraphPreferences> {
Ok(pb::GraphPreferences {
pub(crate) fn get_graph_preferences(&self) -> pb::GraphPreferences {
pb::GraphPreferences {
calendar_first_day_of_week: self.get_first_day_of_week() as i32,
card_counts_separate_inactive: self.get_bool(BoolKey::CardCountsSeparateInactive),
browser_links_supported: true,
future_due_show_backlog: self.get_bool(BoolKey::FutureDueShowBacklog),
})
}
}
pub(crate) fn set_graph_preferences(&mut self, prefs: pb::GraphPreferences) -> Result<()> {

View file

@ -127,7 +127,9 @@ fn schema_version(db: &Connection) -> Result<(bool, u8)> {
Ok((
false,
db.query_row("select ver from col", NO_PARAMS, |r| Ok(r.get(0)?))?,
db.query_row("select ver from col", NO_PARAMS, |r| {
r.get(0).map_err(Into::into)
})?,
))
}

View file

@ -228,8 +228,8 @@ impl Collection {
/// Return changes made by the current op. Must only be called in a transaction,
/// when an operation was passed to transact().
pub(crate) fn op_changes(&self) -> Result<OpChanges> {
Ok(self.state.undo.op_changes())
pub(crate) fn op_changes(&self) -> OpChanges {
self.state.undo.op_changes()
}
}