Skip to content

Commit 2e9d173

Browse files
author
Michael Wright
committed
literal representation restructure 12
Export function for formatting literals and remove crate visibility from other items.
1 parent eb9caf3 commit 2e9d173

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

clippy_lints/src/excessive_precision.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::utils::span_lint_and_sugg;
2+
use crate::utils::sugg::format_numeric_literal;
23
use if_chain::if_chain;
34
use rustc::hir;
45
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
@@ -86,8 +87,7 @@ impl ExcessivePrecision {
8687
if sym_str == s {
8788
None
8889
} else {
89-
let num_lit = super::literal_representation::NumericLiteral::new(&s, None, true);
90-
Some(num_lit.format())
90+
Some(format_numeric_literal(&s, None, true))
9191
}
9292
} else {
9393
None

clippy_lints/src/literal_representation.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,20 +114,26 @@ pub(super) enum Radix {
114114
impl Radix {
115115
/// Returns a reasonable digit group size for this radix.
116116
#[must_use]
117-
crate fn suggest_grouping(&self) -> usize {
117+
fn suggest_grouping(&self) -> usize {
118118
match *self {
119119
Self::Binary | Self::Hexadecimal => 4,
120120
Self::Octal | Self::Decimal => 3,
121121
}
122122
}
123123
}
124124

125+
/// A helper method to format numeric literals with digit grouping.
126+
/// `lit` must be a valid numeric literal without suffix.
127+
pub fn format_numeric_literal(lit: &str, type_suffix: Option<&str>, float: bool) -> String {
128+
NumericLiteral::new(lit, type_suffix, float).format()
129+
}
130+
125131
#[derive(Debug)]
126132
pub(super) struct NumericLiteral<'a> {
127133
/// Which radix the literal was represented in.
128-
crate radix: Radix,
134+
radix: Radix,
129135
/// The radix prefix, if present.
130-
crate prefix: Option<&'a str>,
136+
prefix: Option<&'a str>,
131137

132138
/// The integer part of the number.
133139
integer: &'a str,
@@ -137,7 +143,7 @@ pub(super) struct NumericLiteral<'a> {
137143
exponent: Option<(char, &'a str)>,
138144

139145
/// The type suffix, including preceding underscore if present.
140-
crate suffix: Option<&'a str>,
146+
suffix: Option<&'a str>,
141147
}
142148

143149
impl<'a> NumericLiteral<'a> {
@@ -152,7 +158,7 @@ impl<'a> NumericLiteral<'a> {
152158
}
153159

154160
#[must_use]
155-
crate fn new(lit: &'a str, suffix: Option<&'a str>, float: bool) -> Self {
161+
fn new(lit: &'a str, suffix: Option<&'a str>, float: bool) -> Self {
156162
// Determine delimiter for radix prefix, if present, and radix.
157163
let radix = if lit.starts_with("0x") {
158164
Radix::Hexadecimal
@@ -219,7 +225,7 @@ impl<'a> NumericLiteral<'a> {
219225
}
220226

221227
/// Returns literal formatted in a sensible way.
222-
crate fn format(&self) -> String {
228+
fn format(&self) -> String {
223229
let mut output = String::new();
224230

225231
if let Some(prefix) = self.prefix {
@@ -324,7 +330,7 @@ enum WarningType {
324330
}
325331

326332
impl WarningType {
327-
crate fn display(&self, suggested_format: String, cx: &EarlyContext<'_>, span: syntax_pos::Span) {
333+
fn display(&self, suggested_format: String, cx: &EarlyContext<'_>, span: syntax_pos::Span) {
328334
match self {
329335
Self::MistypedLiteralSuffix => span_lint_and_sugg(
330336
cx,

clippy_lints/src/utils/sugg.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ use syntax::token;
1818
use syntax::util::parser::AssocOp;
1919
use syntax_pos::{BytePos, Pos};
2020

21+
pub use crate::literal_representation::format_numeric_literal;
22+
2123
/// A helper type to build suggestion correctly handling parenthesis.
2224
pub enum Sugg<'a> {
2325
/// An expression that never needs parenthesis such as `1337` or `[0; 42]`.

0 commit comments

Comments
 (0)