Skip to content

Commit 5325b9e

Browse files
ding-youngytmimi
authored andcommitted
modify rewrite_literal
1 parent cf352a7 commit 5325b9e

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

src/attr.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::config::IndentStyle;
1111
use crate::expr::rewrite_literal;
1212
use crate::lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator};
1313
use crate::overflow;
14-
use crate::rewrite::{Rewrite, RewriteContext, RewriteErrorExt};
14+
use crate::rewrite::{Rewrite, RewriteContext, RewriteErrorExt, RewriteResult};
1515
use crate::shape::Shape;
1616
use crate::source_map::SpanUtils;
1717
use crate::types::{rewrite_path, PathContext};
@@ -244,8 +244,14 @@ fn rewrite_initial_doc_comments(
244244

245245
impl Rewrite for ast::NestedMetaItem {
246246
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
247+
self.rewrite_result(context, shape).ok()
248+
}
249+
250+
fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
247251
match self {
248-
ast::NestedMetaItem::MetaItem(ref meta_item) => meta_item.rewrite(context, shape),
252+
ast::NestedMetaItem::MetaItem(ref meta_item) => {
253+
meta_item.rewrite_result(context, shape)
254+
}
249255
ast::NestedMetaItem::Lit(ref l) => {
250256
rewrite_literal(context, l.as_token_lit(), l.span, shape)
251257
}
@@ -277,11 +283,7 @@ impl Rewrite for ast::MetaItem {
277283
self.rewrite_result(context, shape).ok()
278284
}
279285

280-
fn rewrite_result(
281-
&self,
282-
context: &RewriteContext<'_>,
283-
shape: Shape,
284-
) -> crate::rewrite::RewriteResult {
286+
fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
285287
Ok(match self.kind {
286288
ast::MetaItemKind::Word => {
287289
rewrite_path(context, PathContext::Type, &None, &self.path, shape)?
@@ -317,7 +319,7 @@ impl Rewrite for ast::MetaItem {
317319
// is longer than the max width and continue on formatting.
318320
// See #2479 for example.
319321
let value = rewrite_literal(context, lit.as_token_lit(), lit.span, lit_shape)
320-
.unwrap_or_else(|| context.snippet(lit.span).to_owned());
322+
.unwrap_or_else(|_| context.snippet(lit.span).to_owned());
321323
format!("{path} = {value}")
322324
}
323325
})

src/expr.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ pub(crate) fn format_expr(
8282
)
8383
.ok(),
8484
ast::ExprKind::Lit(token_lit) => {
85-
if let Some(expr_rw) = rewrite_literal(context, token_lit, expr.span, shape) {
85+
if let Ok(expr_rw) = rewrite_literal(context, token_lit, expr.span, shape) {
8686
Some(expr_rw)
8787
} else {
8888
if let LitKind::StrRaw(_) = token_lit.kind {
@@ -1262,19 +1262,20 @@ pub(crate) fn rewrite_literal(
12621262
token_lit: token::Lit,
12631263
span: Span,
12641264
shape: Shape,
1265-
) -> Option<String> {
1265+
) -> RewriteResult {
12661266
match token_lit.kind {
12671267
token::LitKind::Str => rewrite_string_lit(context, span, shape),
12681268
token::LitKind::Integer => rewrite_int_lit(context, token_lit, span, shape),
12691269
_ => wrap_str(
12701270
context.snippet(span).to_owned(),
12711271
context.config.max_width(),
12721272
shape,
1273-
),
1273+
)
1274+
.max_width_error(shape.width, span),
12741275
}
12751276
}
12761277

1277-
fn rewrite_string_lit(context: &RewriteContext<'_>, span: Span, shape: Shape) -> Option<String> {
1278+
fn rewrite_string_lit(context: &RewriteContext<'_>, span: Span, shape: Shape) -> RewriteResult {
12781279
let string_lit = context.snippet(span);
12791280

12801281
if !context.config.format_strings() {
@@ -1284,9 +1285,10 @@ fn rewrite_string_lit(context: &RewriteContext<'_>, span: Span, shape: Shape) ->
12841285
.all(|line| line.ends_with('\\'))
12851286
&& context.config.style_edition() >= StyleEdition::Edition2024
12861287
{
1287-
return Some(string_lit.to_owned());
1288+
return Ok(string_lit.to_owned());
12881289
} else {
1289-
return wrap_str(string_lit.to_owned(), context.config.max_width(), shape);
1290+
return wrap_str(string_lit.to_owned(), context.config.max_width(), shape)
1291+
.max_width_error(shape.width, span);
12901292
}
12911293
}
12921294

@@ -1298,14 +1300,15 @@ fn rewrite_string_lit(context: &RewriteContext<'_>, span: Span, shape: Shape) ->
12981300
&StringFormat::new(shape.visual_indent(0), context.config),
12991301
shape.width.saturating_sub(2),
13001302
)
1303+
.max_width_error(shape.width, span) // - 2 ?
13011304
}
13021305

13031306
fn rewrite_int_lit(
13041307
context: &RewriteContext<'_>,
13051308
token_lit: token::Lit,
13061309
span: Span,
13071310
shape: Shape,
1308-
) -> Option<String> {
1311+
) -> RewriteResult {
13091312
let symbol = token_lit.symbol.as_str();
13101313

13111314
if let Some(symbol_stripped) = symbol.strip_prefix("0x") {
@@ -1323,7 +1326,8 @@ fn rewrite_int_lit(
13231326
),
13241327
context.config.max_width(),
13251328
shape,
1326-
);
1329+
)
1330+
.max_width_error(shape.width, span);
13271331
}
13281332
}
13291333

@@ -1332,6 +1336,7 @@ fn rewrite_int_lit(
13321336
context.config.max_width(),
13331337
shape,
13341338
)
1339+
.max_width_error(shape.width, span)
13351340
}
13361341

13371342
fn choose_separator_tactic(context: &RewriteContext<'_>, span: Span) -> Option<SeparatorTactic> {

0 commit comments

Comments
 (0)