Repository: paritytech/substrate
File: frame/balances/src/lib.rs (Rust)

Code before:

		let existed = Locks::<T, I>::contains_key(who);
		if locks.is_empty() {
			Locks::<T, I>::remove(who);
			if existed {
				// TODO: use Locks::<T, I>::hashed_key
				// https://github.com/paritytech/substrate/issues/4969
				system::Pallet::<T>::dec_consumers(who);
			}
		} else {
			Locks::<T, I>::insert(who, bounded_locks);
			if !existed && system::Pallet::<T>::inc_consumers_without_limit(who).is_err() {
				// No providers for the locks. This is impossible under normal circumstances
				// since the funds that are under the lock will themselves be stored in the
				// account and therefore will need a reference.
				log::warn!(
					target: "runtime::balances",
					"Warning: Attempt to introduce lock consumer reference, yet no providers. \
					This is unexpected but should be safe."
				);
			}
		}

		let deposit_lock_event = |prev: T::Balance, after: T::Balance, reason: Reasons| {
			if prev < after {
				let amount = after - prev;
				Self::deposit_event(Event::Locked { who: who.clone(), amount, reason });
			} else if prev > after {
				let amount = prev - after;
				Self::deposit_event(Event::Unlocked { who: who.clone(), amount, reason });
			}
		};

		deposit_lock_event(prev_misc_frozen, after_misc_frozen, Reasons::Misc);
		deposit_lock_event(prev_fee_frozen, after_fee_frozen, Reasons::Fee);
	}

	/// Move the reserved balance of one account into the balance of another, according to `status`.
	///
	/// Is a no-op if:
	/// - the value to be moved is zero; or
	/// - the `slashed` id equal to `beneficiary` and the `status` is `Reserved`.
	///
	/// NOTE: returns actual amount of transferred value in `Ok` case.
	fn do_transfer_reserved(
		slashed: &T::AccountId,
		beneficiary: &T::AccountId,
		value: T::Balance,
		best_effort: bool,
		status: Status,
	) -> Result<T::Balance, DispatchError> {

Review comment (suggestion):
```suggestion
				let amount = after.saturaitng_sub(prev);
```

Code after:
					None
				};
				Ok((maybe_endowed, maybe_dust, result))
			});
			result.map(|(maybe_endowed, maybe_dust, result)| {
				if let Some(endowed) = maybe_endowed {
					Self::deposit_event(Event::Endowed {
						account: who.clone(),
						free_balance: endowed,
					});
				}
				if let Some(amount) = maybe_dust {
					Pallet::<T, I>::deposit_event(Event::DustLost { account: who.clone(), amount });
				}
				(result, maybe_dust)
			})
		}

		/// Update the account entry for `who`, given the locks.
		pub(crate) fn update_locks(who: &T::AccountId, locks: &[BalanceLock<T::Balance>]) {
			let bounded_locks = WeakBoundedVec::<_, T::MaxLocks>::force_from(
				locks.to_vec(),
				Some("Balances Update Locks"),
			);

			if locks.len() as u32 > T::MaxLocks::get() {
				log::warn!(
					target: LOG_TARGET,
					"Warning: A user has more currency locks than expected. \
					A runtime configuration adjustment may be needed."
				);
			}
			let freezes = Freezes::<T, I>::get(who);
			let mut prev_frozen = Zero::zero();
			let mut after_frozen = Zero::zero();
			// TODO: Revisit this assumption. We no manipulate consumer/provider refs.
			// No way this can fail since we do not alter the existential balances.
			let res = Self::mutate_account(who, |b| {
				prev_frozen = b.frozen;
				b.frozen = Zero::zero();
				for l in locks.iter() {
					b.frozen = b.frozen.max(l.amount);
				}
				for l in freezes.iter() {
					b.frozen = b.frozen.max(l.amount);
				}
				after_frozen = b.frozen;
			});
			debug_assert!(res.is_ok());
			if let Ok((_, maybe_dust)) = res {
				debug_assert!(maybe_dust.is_none(), "Not altering main balance; qed");