Skip to content

Commit

Permalink
Error on zero tab width (astral-sh#6429)
Browse files Browse the repository at this point in the history
## Summary

Error if `tab-size` is set to zero (it is used as a divisor). Closes
astral-sh#6423.

Also fixes a typo.

## Test Plan

Running ruff with a config

```toml
[tool.ruff]
tab-size = 0
```

returns an error message to the user saying that `tab-size` must be
greater than zero.
  • Loading branch information
tjkuson authored and durumu committed Aug 12, 2023
1 parent dca92dd commit 79763d7
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 19 deletions.
23 changes: 12 additions & 11 deletions crates/ruff/src/line_width.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use serde::{Deserialize, Serialize};
use std::num::NonZeroU8;
use unicode_width::UnicodeWidthChar;

use ruff_macros::CacheKey;
Expand Down Expand Up @@ -83,7 +84,7 @@ impl LineWidth {
}

fn update(mut self, chars: impl Iterator<Item = char>) -> Self {
let tab_size: usize = self.tab_size.into();
let tab_size: usize = self.tab_size.as_usize();
for c in chars {
match c {
'\t' => {
Expand Down Expand Up @@ -144,22 +145,22 @@ impl PartialOrd<LineLength> for LineWidth {
/// The size of a tab.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, CacheKey)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub struct TabSize(pub u8);
pub struct TabSize(pub NonZeroU8);

impl Default for TabSize {
fn default() -> Self {
Self(4)
impl TabSize {
fn as_usize(self) -> usize {
self.0.get() as usize
}
}

impl From<u8> for TabSize {
fn from(tab_size: u8) -> Self {
Self(tab_size)
impl Default for TabSize {
fn default() -> Self {
Self(NonZeroU8::new(4).unwrap())
}
}

impl From<TabSize> for usize {
fn from(tab_size: TabSize) -> Self {
tab_size.0 as usize
impl From<NonZeroU8> for TabSize {
fn from(tab_size: NonZeroU8) -> Self {
Self(tab_size)
}
}
4 changes: 1 addition & 3 deletions crates/ruff/src/message/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,12 +293,10 @@ impl Display for MessageCodeFrame<'_> {
}

fn replace_whitespace(source: &str, annotation_range: TextRange) -> SourceCode {
static TAB_SIZE: TabSize = TabSize(4); // TODO(jonathan): use `tab-size`

let mut result = String::new();
let mut last_end = 0;
let mut range = annotation_range;
let mut line_width = LineWidth::new(TAB_SIZE);
let mut line_width = LineWidth::new(TabSize::default());

for (index, c) in source.char_indices() {
let old_width = line_width.get();
Expand Down
3 changes: 2 additions & 1 deletion crates/ruff/src/rules/pycodestyle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub(crate) mod helpers;

#[cfg(test)]
mod tests {
use std::num::NonZeroU8;
use std::path::Path;

use anyhow::Result;
Expand Down Expand Up @@ -204,7 +205,7 @@ mod tests {
let diagnostics = test_path(
Path::new("pycodestyle/E501_2.py"),
&settings::Settings {
tab_size: tab_size.into(),
tab_size: NonZeroU8::new(tab_size).unwrap().into(),
..settings::Settings::for_rule(Rule::LineTooLong)
},
)?;
Expand Down
4 changes: 2 additions & 2 deletions crates/ruff/src/settings/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,13 +312,13 @@ pub struct Options {
"#
)]
/// The line length to use when enforcing long-lines violations (like
/// `E501`).
/// `E501`). Must be greater than `0`.
pub line_length: Option<LineLength>,
#[option(
default = "4",
value_type = "int",
example = r#"
tab_size = 8
tab-size = 8
"#
)]
/// The tabulation size to calculate line length.
Expand Down
8 changes: 8 additions & 0 deletions crates/ruff_cache/src/cache_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::borrow::Cow;
use std::collections::hash_map::DefaultHasher;
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::hash::{Hash, Hasher};
use std::num::NonZeroU8;
use std::ops::{Deref, DerefMut};
use std::path::{Path, PathBuf};

Expand Down Expand Up @@ -205,6 +206,13 @@ impl CacheKey for i8 {
}
}

impl CacheKey for NonZeroU8 {
#[inline]
fn cache_key(&self, state: &mut CacheKeyHasher) {
state.write_u8(self.get());
}
}

macro_rules! impl_cache_key_tuple {
() => (
impl CacheKey for () {
Expand Down
4 changes: 2 additions & 2 deletions ruff.schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 79763d7

Please sign in to comment.