From: Mirebella <166803555+Mirebella@users.noreply.github.com> Date: Tue, 13 Aug 2024 11:41:16 +0000 (+0200) Subject: Add clippy ignore rules for all errors and warnings X-Git-Tag: v0.0.124-rc1~3^2~1 X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=6ff1978eba95b40499e06d20695a99420818df6c;p=rust-lightning Add clippy ignore rules for all errors and warnings --- diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 9ed1b5eee..989e51e83 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -251,7 +251,94 @@ jobs: rustup component add clippy - name: Run default clippy linting run: | - cargo clippy -- -Aclippy::erasing_op -Aclippy::never_loop -Aclippy::if_same_then_else -Dclippy::try_err + RUSTFLAGS='-D warnings' cargo clippy -- \ + `# Errors` \ + -A clippy::erasing_op \ + -A clippy::never_loop \ + `# Warnings` \ + -A renamed_and_removed_lints \ + -A clippy::blocks_in_conditions \ + -A clippy::borrow_deref_ref \ + -A clippy::clone_on_copy \ + -A clippy::collapsible_else_if \ + -A clippy::collapsible_if \ + -A clippy::collapsible_match \ + -A clippy::comparison_chain \ + -A clippy::drain_collect \ + -A clippy::drop_non_drop \ + -A clippy::enum_variant_names \ + -A clippy::explicit_auto_deref \ + -A clippy::extra_unused_lifetimes \ + -A clippy::for_kv_map \ + -A clippy::from_over_into \ + -A clippy::get_first \ + -A clippy::identity_op \ + -A clippy::if_same_then_else \ + -A clippy::inconsistent_digit_grouping \ + -A clippy::iter_kv_map \ + -A clippy::iter_skip_next \ + -A clippy::large_enum_variant \ + -A clippy::legacy_numeric_constants \ + -A clippy::len_without_is_empty \ + -A clippy::len_zero \ + -A clippy::let_and_return \ + -A clippy::manual_filter \ + -A clippy::manual_map \ + -A clippy::manual_memcpy \ + -A clippy::manual_range_contains \ + -A clippy::manual_range_patterns \ + -A clippy::manual_saturating_arithmetic \ + -A clippy::manual_strip \ + -A clippy::map_clone \ + -A clippy::map_flatten \ + -A clippy::match_like_matches_macro \ + -A clippy::match_ref_pats \ + -A clippy::multiple_bound_locations \ + -A clippy::mut_mutex_lock \ + -A clippy::needless_bool \ + -A clippy::needless_borrow \ + -A clippy::needless_borrowed_reference \ + -A clippy::needless_borrows_for_generic_args \ + -A clippy::needless_lifetimes \ + -A clippy::needless_question_mark \ + -A clippy::needless_range_loop \ + -A clippy::needless_return \ + -A clippy::new_without_default \ + -A clippy::non_minimal_cfg \ + -A clippy::op_ref \ + -A clippy::option_as_ref_deref \ + -A clippy::option_map_or_none \ + -A clippy::option_map_unit_fn \ + -A clippy::precedence \ + -A clippy::ptr_arg \ + -A clippy::question_mark \ + -A clippy::readonly_write_lock \ + -A clippy::redundant_closure \ + -A clippy::redundant_field_names \ + -A clippy::redundant_guards \ + -A clippy::redundant_pattern_matching \ + -A clippy::redundant_slicing \ + -A clippy::redundant_static_lifetimes \ + -A clippy::result_large_err \ + -A clippy::result_unit_err \ + -A clippy::search_is_some \ + -A clippy::single_char_pattern \ + -A clippy::single_match \ + -A clippy::slow_vector_initialization \ + -A clippy::tabs_in_doc_comments \ + -A clippy::to_string_in_format_args \ + -A clippy::too_many_arguments \ + -A clippy::toplevel_ref_arg \ + -A clippy::type_complexity \ + -A clippy::unnecessary_cast \ + -A clippy::unnecessary_get_then_check \ + -A clippy::unnecessary_lazy_evaluations \ + -A clippy::unnecessary_mut_passed \ + -A clippy::unnecessary_sort_by \ + -A clippy::unnecessary_to_owned \ + -A clippy::unnecessary_unwrap \ + -A clippy::unused_unit \ + -A clippy::useless_conversion rustfmt: runs-on: ubuntu-latest diff --git a/lightning-rapid-gossip-sync/src/processing.rs b/lightning-rapid-gossip-sync/src/processing.rs index c7cda96ff..a9757e04d 100644 --- a/lightning-rapid-gossip-sync/src/processing.rs +++ b/lightning-rapid-gossip-sync/src/processing.rs @@ -176,7 +176,7 @@ where .and_then(|node| node.announcement_info.as_ref()) .map(|info| { synthetic_node_announcement.features = info.features().clone(); - synthetic_node_announcement.rgb = info.rgb().clone(); + synthetic_node_announcement.rgb.clone_from(&info.rgb()); synthetic_node_announcement.alias = info.alias().clone(); synthetic_node_announcement.addresses = info.addresses().to_vec(); }); diff --git a/lightning/src/chain/onchaintx.rs b/lightning/src/chain/onchaintx.rs index eff827983..b07297ce2 100644 --- a/lightning/src/chain/onchaintx.rs +++ b/lightning/src/chain/onchaintx.rs @@ -761,7 +761,7 @@ impl OnchainTxHandler { for outpoint in req.outpoints() { log_info!(logger, " Outpoint {}", outpoint); } - self.locktimed_packages.entry(package_locktime).or_insert(Vec::new()).push(req); + self.locktimed_packages.entry(package_locktime).or_default().push(req); continue; } diff --git a/lightning/src/chain/package.rs b/lightning/src/chain/package.rs index 361e12fa5..ce3ac1d9d 100644 --- a/lightning/src/chain/package.rs +++ b/lightning/src/chain/package.rs @@ -1033,8 +1033,7 @@ impl PackageTemplate { pub (crate) fn build_package(txid: Txid, vout: u32, input_solving_data: PackageSolvingData, soonest_conf_deadline: u32, height_original: u32) -> Self { let (malleability, aggregable) = PackageSolvingData::map_output_type_flags(&input_solving_data); - let mut inputs = Vec::with_capacity(1); - inputs.push((BitcoinOutPoint { txid, vout }, input_solving_data)); + let inputs = vec![(BitcoinOutPoint { txid, vout }, input_solving_data)]; PackageTemplate { inputs, malleability, diff --git a/lightning/src/crypto/chacha20.rs b/lightning/src/crypto/chacha20.rs index 0c51d4562..d1dcff147 100644 --- a/lightning/src/crypto/chacha20.rs +++ b/lightning/src/crypto/chacha20.rs @@ -176,7 +176,7 @@ mod real_chacha { ) { let block = ChaCha20::get_single_block(key, nonce); for i in 0..bytes.len() { - bytes[i] = block[i] ^ bytes[i]; + bytes[i] ^= block[i]; } } diff --git a/lightning/src/crypto/chacha20poly1305rfc.rs b/lightning/src/crypto/chacha20poly1305rfc.rs index 4ca974421..76d6040c2 100644 --- a/lightning/src/crypto/chacha20poly1305rfc.rs +++ b/lightning/src/crypto/chacha20poly1305rfc.rs @@ -59,7 +59,7 @@ mod real_chachapoly { pub fn encrypt(&mut self, input: &[u8], output: &mut [u8], out_tag: &mut [u8]) { assert!(input.len() == output.len()); - assert!(self.finished == false); + assert!(!self.finished); self.cipher.process(input, output); self.data_len += input.len(); self.mac.input(output); @@ -78,7 +78,7 @@ mod real_chachapoly { // Encrypt `input_output` in-place. To finish and calculate the tag, use `finish_and_get_tag` // below. pub(in super::super) fn encrypt_in_place(&mut self, input_output: &mut [u8]) { - debug_assert!(self.finished == false); + debug_assert!(!self.finished); self.cipher.process_in_place(input_output); self.data_len += input_output.len(); self.mac.input(input_output); @@ -87,7 +87,7 @@ mod real_chachapoly { // If we were previously encrypting with `encrypt_in_place`, this method can be used to finish // encrypting and calculate the tag. pub(in super::super) fn finish_and_get_tag(&mut self, out_tag: &mut [u8]) { - debug_assert!(self.finished == false); + debug_assert!(!self.finished); ChaCha20Poly1305RFC::pad_mac_16(&mut self.mac, self.data_len); self.finished = true; self.mac.input(&self.aad_len.to_le_bytes()); @@ -100,7 +100,7 @@ mod real_chachapoly { /// this decryption is *variable time*. pub fn variable_time_decrypt(&mut self, input: &[u8], output: &mut [u8], tag: &[u8]) -> Result<(), ()> { assert!(input.len() == output.len()); - assert!(self.finished == false); + assert!(!self.finished); self.finished = true; @@ -131,7 +131,7 @@ mod real_chachapoly { /// /// Should never be `pub` because the public API should always enforce tag checking. pub(in super::super) fn decrypt_in_place(&mut self, input_output: &mut [u8]) { - debug_assert!(self.finished == false); + debug_assert!(!self.finished); self.mac.input(input_output); self.data_len += input_output.len(); self.cipher.process_in_place(input_output); @@ -140,7 +140,7 @@ mod real_chachapoly { /// If we were previously decrypting with `just_decrypt_in_place`, this method must be used /// to check the tag. Returns whether or not the tag is valid. pub(in super::super) fn finish_and_check_tag(&mut self, tag: &[u8]) -> bool { - debug_assert!(self.finished == false); + debug_assert!(!self.finished); self.finished = true; ChaCha20Poly1305RFC::pad_mac_16(&mut self.mac, self.data_len); self.mac.input(&self.aad_len.to_le_bytes()); diff --git a/lightning/src/crypto/poly1305.rs b/lightning/src/crypto/poly1305.rs index 593200210..7b7449125 100644 --- a/lightning/src/crypto/poly1305.rs +++ b/lightning/src/crypto/poly1305.rs @@ -82,7 +82,7 @@ impl Poly1305 { d2 += c as u64; c = (d2 >> 26) as u32; h2 = d2 as u32 & 0x3ffffff; d3 += c as u64; c = (d3 >> 26) as u32; h3 = d3 as u32 & 0x3ffffff; d4 += c as u64; c = (d4 >> 26) as u32; h4 = d4 as u32 & 0x3ffffff; - h0 += c * 5; c = h0 >> 26; h0 = h0 & 0x3ffffff; + h0 += c * 5; c = h0 >> 26; h0 &= 0x3ffffff; h1 += c; self.h[0] = h0; @@ -111,11 +111,11 @@ impl Poly1305 { let mut h4 = self.h[4]; let mut c : u32; - c = h1 >> 26; h1 = h1 & 0x3ffffff; - h2 += c; c = h2 >> 26; h2 = h2 & 0x3ffffff; - h3 += c; c = h3 >> 26; h3 = h3 & 0x3ffffff; - h4 += c; c = h4 >> 26; h4 = h4 & 0x3ffffff; - h0 += c * 5; c = h0 >> 26; h0 = h0 & 0x3ffffff; + c = h1 >> 26; h1 &= 0x3ffffff; + h2 += c; c = h2 >> 26; h2 &= 0x3ffffff; + h3 += c; c = h3 >> 26; h3 &= 0x3ffffff; + h4 += c; c = h4 >> 26; h4 &= 0x3ffffff; + h0 += c * 5; c = h0 >> 26; h0 &= 0x3ffffff; h1 += c; // compute h + -p diff --git a/lightning/src/events/mod.rs b/lightning/src/events/mod.rs index a823512f1..99ea14b68 100644 --- a/lightning/src/events/mod.rs +++ b/lightning/src/events/mod.rs @@ -1271,6 +1271,7 @@ pub enum Event { /// /// This field will be `None` for objects serialized prior to LDK 0.0.117. channel_capacity_sats: Option, + /// The original channel funding TXO; this helps checking for the existence and confirmation /// status of the closing tx. /// Note that for instances serialized in v0.0.119 or prior this will be missing (None). @@ -2049,7 +2050,7 @@ impl MaybeReadable for Event { payment_hash, purpose: _init_tlv_based_struct_field!(purpose, upgradable_required), amount_msat, - htlcs: htlcs.unwrap_or(vec![]), + htlcs: htlcs.unwrap_or_default(), sender_intended_total_msat, onion_fields, })) diff --git a/lightning/src/ln/chan_utils.rs b/lightning/src/ln/chan_utils.rs index 88b830f16..c40ac3b50 100644 --- a/lightning/src/ln/chan_utils.rs +++ b/lightning/src/ln/chan_utils.rs @@ -221,13 +221,12 @@ pub fn build_commitment_secret(commitment_seed: &[u8; 32], idx: u64) -> [u8; 32] /// Build a closing transaction pub fn build_closing_transaction(to_holder_value_sat: Amount, to_counterparty_value_sat: Amount, to_holder_script: ScriptBuf, to_counterparty_script: ScriptBuf, funding_outpoint: OutPoint) -> Transaction { let txins = { - let mut ins: Vec = Vec::new(); - ins.push(TxIn { + let ins: Vec = vec![TxIn { previous_output: funding_outpoint, script_sig: ScriptBuf::new(), sequence: Sequence::MAX, witness: Witness::new(), - }); + }]; ins }; @@ -702,8 +701,7 @@ pub(crate) fn make_funding_redeemscript_from_slices(broadcaster_funding_key: &[u /// Panics if htlc.transaction_output_index.is_none() (as such HTLCs do not appear in the /// commitment transaction). pub fn build_htlc_transaction(commitment_txid: &Txid, feerate_per_kw: u32, contest_delay: u16, htlc: &HTLCOutputInCommitment, channel_type_features: &ChannelTypeFeatures, broadcaster_delayed_payment_key: &DelayedPaymentKey, revocation_key: &RevocationKey) -> Transaction { - let mut txins: Vec = Vec::new(); - txins.push(build_htlc_input(commitment_txid, htlc, channel_type_features)); + let txins= vec![build_htlc_input(commitment_txid, htlc, channel_type_features)]; let mut txouts: Vec = Vec::new(); txouts.push(build_htlc_output( @@ -1590,14 +1588,13 @@ impl CommitmentTransaction { commitment_transaction_number_obscure_factor ^ (INITIAL_COMMITMENT_NUMBER - commitment_number); let txins = { - let mut ins: Vec = Vec::new(); - ins.push(TxIn { + let ins: Vec = vec![TxIn { previous_output: channel_parameters.funding_outpoint(), script_sig: ScriptBuf::new(), sequence: Sequence(((0x80 as u32) << 8 * 3) | ((obscured_commitment_transaction_number >> 3 * 8) as u32)), witness: Witness::new(), - }); + }]; ins }; (obscured_commitment_transaction_number, txins) diff --git a/lightning/src/ln/channel.rs b/lightning/src/ln/channel.rs index 576ae665a..ad6981f7c 100644 --- a/lightning/src/ln/channel.rs +++ b/lightning/src/ln/channel.rs @@ -610,7 +610,7 @@ impl ChannelState { } } - fn to_u32(&self) -> u32 { + fn to_u32(self) -> u32 { match self { ChannelState::NegotiatingFunding(flags) => flags.0, ChannelState::FundingNegotiated => state_flags::FUNDING_NEGOTIATED, @@ -9505,7 +9505,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch monitor_pending_forwards, monitor_pending_failures, monitor_pending_finalized_fulfills: monitor_pending_finalized_fulfills.unwrap(), - monitor_pending_update_adds: monitor_pending_update_adds.unwrap_or(Vec::new()), + monitor_pending_update_adds: monitor_pending_update_adds.unwrap_or_default(), signer_pending_revoke_and_ack: false, signer_pending_commitment_update: false, diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index b14369c43..627aa50de 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -5165,7 +5165,7 @@ where failure_code: fail_malformed_htlc.failure_code, }, }; - self.forward_htlcs.lock().unwrap().entry(incoming_scid).or_insert(vec![]).push(failure); + self.forward_htlcs.lock().unwrap().entry(incoming_scid).or_default().push(failure); self.pending_events.lock().unwrap().push_back((events::Event::HTLCHandlingFailed { prev_channel_id: incoming_channel_id, failed_next_destination: htlc_destination, @@ -6770,7 +6770,7 @@ where peer_state.actions_blocking_raa_monitor_updates .entry(prev_hop.channel_id) - .or_insert_with(Vec::new) + .or_default() .push(raa_blocker); } else { debug_assert!(false, diff --git a/lightning/src/ln/peer_handler.rs b/lightning/src/ln/peer_handler.rs index dda1ca8b9..51695bba0 100644 --- a/lightning/src/ln/peer_handler.rs +++ b/lightning/src/ln/peer_handler.rs @@ -1431,7 +1431,7 @@ impl 0); diff --git a/lightning/src/offers/invoice.rs b/lightning/src/offers/invoice.rs index 40ee40090..865939b07 100644 --- a/lightning/src/offers/invoice.rs +++ b/lightning/src/offers/invoice.rs @@ -1018,7 +1018,7 @@ impl InvoiceContents { self.fields().fallbacks .as_ref() .map(|fallbacks| filter_fallbacks(self.chain(), fallbacks)) - .unwrap_or_else(Vec::new) + .unwrap_or_default() } fn features(&self) -> &Bolt12InvoiceFeatures { diff --git a/lightning/src/offers/offer.rs b/lightning/src/offers/offer.rs index 96cd4465c..27a79166b 100644 --- a/lightning/src/offers/offer.rs +++ b/lightning/src/offers/offer.rs @@ -1048,7 +1048,7 @@ pub enum Quantity { } impl Quantity { - fn to_tlv_record(&self) -> Option { + fn to_tlv_record(self) -> Option { match self { Quantity::Bounded(n) => Some(n.get()), Quantity::Unbounded => Some(0), diff --git a/lightning/src/offers/signer.rs b/lightning/src/offers/signer.rs index 2ee54c588..ebab9b072 100644 --- a/lightning/src/offers/signer.rs +++ b/lightning/src/offers/signer.rs @@ -240,7 +240,7 @@ impl MetadataMaterial { self.hmac.input(DERIVED_METADATA_HMAC_INPUT); self.maybe_include_encrypted_payment_id(); - let mut bytes = self.encrypted_payment_id.map(|id| id.to_vec()).unwrap_or(vec![]); + let mut bytes = self.encrypted_payment_id.map(|id| id.to_vec()).unwrap_or_default(); bytes.extend_from_slice(self.nonce.as_slice()); bytes.extend_from_slice(Hmac::from_engine(self.hmac).as_byte_array()); bytes @@ -256,7 +256,7 @@ impl MetadataMaterial { self.hmac.input(DERIVED_METADATA_AND_KEYS_HMAC_INPUT); self.maybe_include_encrypted_payment_id(); - let bytes = self.encrypted_payment_id.map(|id| id.to_vec()).unwrap_or(vec![]); + let bytes = self.encrypted_payment_id.map(|id| id.to_vec()).unwrap_or_default(); let hmac = Hmac::from_engine(self.hmac); let privkey = SecretKey::from_slice(hmac.as_byte_array()).unwrap(); diff --git a/lightning/src/routing/gossip.rs b/lightning/src/routing/gossip.rs index 1acd2ebdc..f1bc73110 100644 --- a/lightning/src/routing/gossip.rs +++ b/lightning/src/routing/gossip.rs @@ -509,10 +509,10 @@ where U::Target: UtxoLookup, L::Target: Logger let mut one_to_two_announcement: Option = None; let mut two_to_one_announcement: Option = None; if let Some(one_to_two) = chan.one_to_two.as_ref() { - one_to_two_announcement = one_to_two.last_update_message.clone(); + one_to_two_announcement.clone_from(&one_to_two.last_update_message); } if let Some(two_to_one) = chan.two_to_one.as_ref() { - two_to_one_announcement = two_to_one.last_update_message.clone(); + two_to_one_announcement.clone_from(&two_to_one.last_update_message); } return Some((chan_announcement, one_to_two_announcement, two_to_one_announcement)); } else { diff --git a/lightning/src/routing/router.rs b/lightning/src/routing/router.rs index 65ac21ee4..49760aa12 100644 --- a/lightning/src/routing/router.rs +++ b/lightning/src/routing/router.rs @@ -1704,7 +1704,7 @@ where L::Target: Logger { .filter(|(p, _)| p.blinded_hops().len() == 1) .any(|(_, iter_info_opt)| iter_info_opt.is_some() && iter_info_opt != info_opt) { - return Err(LightningError{err: format!("1-hop blinded paths must all have matching introduction node ids"), action: ErrorAction::IgnoreError}); + return Err(LightningError{err: "1-hop blinded paths must all have matching introduction node ids".to_string(), action: ErrorAction::IgnoreError}); } } } diff --git a/lightning/src/routing/scoring.rs b/lightning/src/routing/scoring.rs index 480cbc5b3..d40b1f22c 100644 --- a/lightning/src/routing/scoring.rs +++ b/lightning/src/routing/scoring.rs @@ -1587,7 +1587,7 @@ mod bucketed_history { } impl LegacyHistoricalBucketRangeTracker { - pub(crate) fn into_current(&self) -> HistoricalBucketRangeTracker { + pub(crate) fn into_current(self) -> HistoricalBucketRangeTracker { let mut buckets = [0; 32]; for (idx, legacy_bucket) in self.buckets.iter().enumerate() { let mut new_val = *legacy_bucket; diff --git a/lightning/src/routing/utxo.rs b/lightning/src/routing/utxo.rs index f885c47ef..c96084663 100644 --- a/lightning/src/routing/utxo.rs +++ b/lightning/src/routing/utxo.rs @@ -512,9 +512,9 @@ impl PendingChecks { if let Some(msg) = full_msg { ChannelAnnouncement::Full(msg.clone()) } else { ChannelAnnouncement::Unsigned(msg.clone()) }); pending_checks.nodes.entry(msg.node_id_1) - .or_insert(Vec::new()).push(Arc::downgrade(&future.state)); + .or_default().push(Arc::downgrade(&future.state)); pending_checks.nodes.entry(msg.node_id_2) - .or_insert(Vec::new()).push(Arc::downgrade(&future.state)); + .or_default().push(Arc::downgrade(&future.state)); Err(LightningError { err: "Channel being checked async".to_owned(), action: ErrorAction::IgnoreAndLog(Level::Gossip), diff --git a/lightning/src/util/indexed_map.rs b/lightning/src/util/indexed_map.rs index 97788ffe6..dd29e4ddc 100644 --- a/lightning/src/util/indexed_map.rs +++ b/lightning/src/util/indexed_map.rs @@ -128,11 +128,11 @@ impl IndexedMap { let start = match range.start_bound() { Bound::Unbounded => 0, Bound::Included(key) => self.keys.binary_search(key).unwrap_or_else(|index| index), - Bound::Excluded(key) => self.keys.binary_search(key).and_then(|index| Ok(index + 1)).unwrap_or_else(|index| index), + Bound::Excluded(key) => self.keys.binary_search(key).map(|index| index +1).unwrap_or_else(|index| index), }; let end = match range.end_bound() { Bound::Unbounded => self.keys.len(), - Bound::Included(key) => self.keys.binary_search(key).and_then(|index| Ok(index + 1)).unwrap_or_else(|index| index), + Bound::Included(key) => self.keys.binary_search(key).map(|index| index +1).unwrap_or_else(|index| index), Bound::Excluded(key) => self.keys.binary_search(key).unwrap_or_else(|index| index), }; diff --git a/lightning/src/util/logger.rs b/lightning/src/util/logger.rs index 33ef9b1f7..cba5dbb77 100644 --- a/lightning/src/util/logger.rs +++ b/lightning/src/util/logger.rs @@ -247,7 +247,7 @@ impl + Clone> fmt::Display fo if let Some(item) = iter.next() { write!(f, "{}", item)?; } - while let Some(item) = iter.next() { + for item in iter { write!(f, ", {}", item)?; } write!(f, "]")?;