literal representation restructure 12

Export function for formatting literals and remove crate visibility from
other items.
This commit is contained in:
Michael Wright 2019-11-13 08:28:50 +02:00
parent eb9caf3050
commit 2e9d173be1
3 changed files with 17 additions and 9 deletions

View file

@ -1,4 +1,5 @@
use crate::utils::span_lint_and_sugg;
use crate::utils::sugg::format_numeric_literal;
use if_chain::if_chain;
use rustc::hir;
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
@ -86,8 +87,7 @@ impl ExcessivePrecision {
if sym_str == s {
None
} else {
let num_lit = super::literal_representation::NumericLiteral::new(&s, None, true);
Some(num_lit.format())
Some(format_numeric_literal(&s, None, true))
}
} else {
None

View file

@ -114,7 +114,7 @@ pub(super) enum Radix {
impl Radix {
/// Returns a reasonable digit group size for this radix.
#[must_use]
crate fn suggest_grouping(&self) -> usize {
fn suggest_grouping(&self) -> usize {
match *self {
Self::Binary | Self::Hexadecimal => 4,
Self::Octal | Self::Decimal => 3,
@ -122,12 +122,18 @@ impl Radix {
}
}
/// A helper method to format numeric literals with digit grouping.
/// `lit` must be a valid numeric literal without suffix.
pub fn format_numeric_literal(lit: &str, type_suffix: Option<&str>, float: bool) -> String {
NumericLiteral::new(lit, type_suffix, float).format()
}
#[derive(Debug)]
pub(super) struct NumericLiteral<'a> {
/// Which radix the literal was represented in.
crate radix: Radix,
radix: Radix,
/// The radix prefix, if present.
crate prefix: Option<&'a str>,
prefix: Option<&'a str>,
/// The integer part of the number.
integer: &'a str,
@ -137,7 +143,7 @@ pub(super) struct NumericLiteral<'a> {
exponent: Option<(char, &'a str)>,
/// The type suffix, including preceding underscore if present.
crate suffix: Option<&'a str>,
suffix: Option<&'a str>,
}
impl<'a> NumericLiteral<'a> {
@ -152,7 +158,7 @@ impl<'a> NumericLiteral<'a> {
}
#[must_use]
crate fn new(lit: &'a str, suffix: Option<&'a str>, float: bool) -> Self {
fn new(lit: &'a str, suffix: Option<&'a str>, float: bool) -> Self {
// Determine delimiter for radix prefix, if present, and radix.
let radix = if lit.starts_with("0x") {
Radix::Hexadecimal
@ -219,7 +225,7 @@ impl<'a> NumericLiteral<'a> {
}
/// Returns literal formatted in a sensible way.
crate fn format(&self) -> String {
fn format(&self) -> String {
let mut output = String::new();
if let Some(prefix) = self.prefix {
@ -324,7 +330,7 @@ enum WarningType {
}
impl WarningType {
crate fn display(&self, suggested_format: String, cx: &EarlyContext<'_>, span: syntax_pos::Span) {
fn display(&self, suggested_format: String, cx: &EarlyContext<'_>, span: syntax_pos::Span) {
match self {
Self::MistypedLiteralSuffix => span_lint_and_sugg(
cx,

View file

@ -18,6 +18,8 @@ use syntax::token;
use syntax::util::parser::AssocOp;
use syntax_pos::{BytePos, Pos};
pub use crate::literal_representation::format_numeric_literal;
/// A helper type to build suggestion correctly handling parenthesis.
pub enum Sugg<'a> {
/// An expression that never needs parenthesis such as `1337` or `[0; 42]`.