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

Code before:
					affected_indices.clone().fold(BalanceOf::<T>::zero(), |sum, i| {
						if let Some(chunk) = self.unlocking.get_mut(i).defensive() {
							sum.saturating_add(chunk.value)
						} else {
							sum
						}
					});
				(
					self.active.saturating_add(unbonding_affected_balance),
					Box::new(affected_indices.chain((0..start_index).rev())),
				)
			} else {
				(self.active, Box::new((0..self.unlocking.len()).rev()))
			}
		};

		// Helper to update `target` and the ledgers total after accounting for slashing `target`.
		let ratio = Perquintill::from_rational(slash_amount, affected_balance);
		let mut slash_out_of = |target: &mut BalanceOf<T>, slash_remaining: &mut BalanceOf<T>| {
			let mut slash_from_target =
				if slash_amount < affected_balance { ratio * (*target) } else { *slash_remaining }
					.min(*target);

			// slash out from *target exactly `slash_from_target`.
			*target = *target - slash_from_target;
			if *target <= minimum_balance {
				// Slash the rest of the target if its dust
				slash_from_target =
					sp_std::mem::replace(target, Zero::zero()).saturating_add(slash_from_target)
			}

			self.total = self.total.saturating_sub(slash_from_target);
			*slash_remaining = slash_remaining.saturating_sub(slash_from_target);
		};

		// If this is *not* a proportional slash, the active will always wiped to 0.
		slash_out_of(&mut self.active, &mut remaining_slash);

		let mut slashed_unlocking = BTreeMap::<_, _>::new();
		for i in slash_chunks_priority {
			if let Some(chunk) = self.unlocking.get_mut(i).defensive() {
				slash_out_of(&mut chunk.value, &mut remaining_slash);
				// write the new slashed value of this chunk to the map.
				slashed_unlocking.insert(chunk.era, chunk.value);
				if remaining_slash.is_zero() {
					break
				}
			} else {
				break
			}
		}

Review comment (suggestion):
```suggestion
			if *target < minimum_balance {
```

Code after:
					affected_indices.clone().fold(BalanceOf::<T>::zero(), |sum, i| {
						if let Some(chunk) = self.unlocking.get_mut(i).defensive() {
							sum.saturating_add(chunk.value)
						} else {
							sum
						}
					});
				(
					self.active.saturating_add(unbonding_affected_balance),
					Box::new(affected_indices.chain((0..start_index).rev())),
				)
			} else {
				(self.active, Box::new((0..self.unlocking.len()).rev()))
			}
		};

		// Helper to update `target` and the ledgers total after accounting for slashing `target`.
		let ratio = Perquintill::from_rational(slash_amount, affected_balance);
		let mut slash_out_of = |target: &mut BalanceOf<T>, slash_remaining: &mut BalanceOf<T>| {
			let mut slash_from_target =
				if slash_amount < affected_balance { ratio * (*target) } else { *slash_remaining }
					.min(*target);

			// slash out from *target exactly `slash_from_target`.
			*target = *target - slash_from_target;
			if *target < minimum_balance {
				// Slash the rest of the target if its dust
				slash_from_target =
					sp_std::mem::replace(target, Zero::zero()).saturating_add(slash_from_target)
			}

			self.total = self.total.saturating_sub(slash_from_target);
			*slash_remaining = slash_remaining.saturating_sub(slash_from_target);
		};

		// If this is *not* a proportional slash, the active will always wiped to 0.
		slash_out_of(&mut self.active, &mut remaining_slash);

		let mut slashed_unlocking = BTreeMap::<_, _>::new();
		for i in slash_chunks_priority {
			if let Some(chunk) = self.unlocking.get_mut(i).defensive() {
				slash_out_of(&mut chunk.value, &mut remaining_slash);
				// write the new slashed value of this chunk to the map.
				slashed_unlocking.insert(chunk.era, chunk.value);
				if remaining_slash.is_zero() {
					break
				}
			} else {
				break
			}
		}